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();
}
});
}
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);
}
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();
}
});
}
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();
}
});
}
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();
}
});
}
Aggregations