Search in sources :

Example 1 with NoHandlerFoundException

use of org.springframework.web.servlet.NoHandlerFoundException in project spring-boot-api-seed-project by selfassu.

the class WebMvcConfigurer method configureHandlerExceptionResolvers.

@Override
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {

        @Nullable
        @Override
        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception exception) {
            Result result = new Result();
            if (exception instanceof ApplicationException) {
                // 业务失败的异常,手动抛出的异常
                result.setCode(ResultCode.FAILED).setMessage(exception.getMessage());
                logger.info(exception.getMessage());
            } else if (exception instanceof NoHandlerFoundException) {
                // 请求路径没有找到
                result.setCode(ResultCode.NOT_FOUND).setMessage("接口 {" + request.getRequestURI() + "} 不存在,请检查");
                logger.info(exception.getMessage());
            } else if (exception instanceof ServletException) {
                result.setCode(ResultCode.FAILED).setMessage(exception.getMessage());
                logger.info(exception.getMessage());
            } else {
                result.setCode(ResultCode.SERVER_ERROR).setMessage("服务器出错!接口 {" + request.getRequestURI() + "} 无法执行,请联系管理员!");
                String message;
                if (handler instanceof HandlerMethod) {
                    HandlerMethod handlerMethod = (HandlerMethod) handler;
                    message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常详细信息:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod().getName(), exception.getMessage());
                } else {
                    message = exception.getMessage();
                }
                logger.error(message, exception);
            }
            responseResult(response, result);
            return new ModelAndView();
        }
    });
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) ApplicationException(com.company.project.core.ApplicationException) HandlerMethod(org.springframework.web.method.HandlerMethod) Result(com.company.project.core.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ApplicationException(com.company.project.core.ApplicationException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) Nullable(org.springframework.lang.Nullable)

Example 2 with NoHandlerFoundException

use of org.springframework.web.servlet.NoHandlerFoundException in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleNoHandlerFoundException.

@Test
public void handleNoHandlerFoundException() throws Exception {
    ServletServerHttpRequest req = new ServletServerHttpRequest(new MockHttpServletRequest("GET", "/resource"));
    NoHandlerFoundException ex = new NoHandlerFoundException(req.getMethod().name(), req.getServletRequest().getRequestURI(), req.getHeaders());
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertThat(mav).as("No ModelAndView returned").isNotNull();
    assertThat(mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
    assertThat(response.getStatus()).as("Invalid status code").isEqualTo(404);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) Test(org.junit.jupiter.api.Test)

Example 3 with NoHandlerFoundException

use of org.springframework.web.servlet.NoHandlerFoundException in project spring-cloud by Rogge666.

the class WebMvcConfigurer method configureHandlerExceptionResolvers.

// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {

        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            ApiResponse lApiResponse = new ApiResponse();
            if (e instanceof ServiceException) {
                // 业务失败的异常,如“账号或密码错误”
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
                logger.info(e.getMessage());
            } else if (e instanceof NoHandlerFoundException) {
                lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
            } else if (e instanceof ServletException) {
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
            } else if (e.getCause() instanceof TooManyResultsException) {
                lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
            } else {
                lApiResponse.setCode(ResponseCode.Base.API_ERR);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
                String message;
                if (handler instanceof HandlerMethod) {
                    HandlerMethod handlerMethod = (HandlerMethod) handler;
                    message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod().getName(), e.getMessage());
                } else {
                    message = e.getMessage();
                }
                logger.error(message, e);
            }
            responseResult(response, lApiResponse);
            return new ModelAndView();
        }
    });
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) IOException(java.io.IOException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) ApiResponse(com.rogge.common.core.ApiResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException)

Example 4 with NoHandlerFoundException

use of org.springframework.web.servlet.NoHandlerFoundException in project spring-cloud by Rogge666.

the class WebMvcConfigurer method configureHandlerExceptionResolvers.

// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {

        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            ApiResponse lApiResponse = new ApiResponse();
            if (e instanceof ServiceException) {
                // 业务失败的异常,如“账号或密码错误”
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
                logger.info(e.getMessage());
            } else if (e instanceof NoHandlerFoundException) {
                lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
            } else if (e instanceof ServletException) {
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
            } else if (e.getCause() instanceof TooManyResultsException) {
                lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
            } else {
                lApiResponse.setCode(ResponseCode.Base.API_ERR);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
                String message;
                if (handler instanceof HandlerMethod) {
                    HandlerMethod handlerMethod = (HandlerMethod) handler;
                    message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod().getName(), e.getMessage());
                } else {
                    message = e.getMessage();
                }
                logger.error(message, e);
            }
            responseResult(response, lApiResponse);
            return new ModelAndView();
        }
    });
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) IOException(java.io.IOException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) ApiResponse(com.rogge.common.core.ApiResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException)

Example 5 with NoHandlerFoundException

use of org.springframework.web.servlet.NoHandlerFoundException in project spring-cloud by Rogge666.

the class WebMvcConfigurer method configureHandlerExceptionResolvers.

// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {

        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            ApiResponse lApiResponse = new ApiResponse();
            if (e instanceof ServiceException) {
                // 业务失败的异常,如“账号或密码错误”
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
                logger.info(e.getMessage());
            } else if (e instanceof NoHandlerFoundException) {
                lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
            } else if (e instanceof ServletException) {
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
            } else if (e.getCause() instanceof TooManyResultsException) {
                lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
            } else {
                lApiResponse.setCode(ResponseCode.Base.API_ERR);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
                String message;
                if (handler instanceof HandlerMethod) {
                    HandlerMethod handlerMethod = (HandlerMethod) handler;
                    message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod().getName(), e.getMessage());
                } else {
                    message = e.getMessage();
                }
                logger.error(message, e);
            }
            responseResult(response, lApiResponse);
            return new ModelAndView();
        }
    });
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) IOException(java.io.IOException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) ApiResponse(com.rogge.common.core.ApiResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException)

Aggregations

NoHandlerFoundException (org.springframework.web.servlet.NoHandlerFoundException)6 ModelAndView (org.springframework.web.servlet.ModelAndView)5 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 HandlerMethod (org.springframework.web.method.HandlerMethod)4 HandlerExceptionResolver (org.springframework.web.servlet.HandlerExceptionResolver)4 ApiResponse (com.rogge.common.core.ApiResponse)3 ServiceException (com.rogge.common.core.ServiceException)3 IOException (java.io.IOException)3 TooManyResultsException (org.apache.ibatis.exceptions.TooManyResultsException)3 Test (org.junit.jupiter.api.Test)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 ApplicationException (com.company.project.core.ApplicationException)1 Result (com.company.project.core.Result)1 ServletException (jakarta.servlet.ServletException)1 ConversionNotSupportedException (org.springframework.beans.ConversionNotSupportedException)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)1