|
|
@ -24,6 +24,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
|
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; |
|
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
@ -62,28 +63,27 @@ public class WebMvcConfigurer extends WebMvcConfigurerAdapter { |
|
|
|
exceptionResolvers.add(new HandlerExceptionResolver() { |
|
|
|
exceptionResolvers.add(new HandlerExceptionResolver() { |
|
|
|
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { |
|
|
|
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { |
|
|
|
Result result = new Result(); |
|
|
|
Result result = new Result(); |
|
|
|
if (handler instanceof HandlerMethod) { |
|
|
|
|
|
|
|
HandlerMethod handlerMethod = (HandlerMethod) handler; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误”
|
|
|
|
if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误”
|
|
|
|
result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); |
|
|
|
result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); |
|
|
|
logger.info(e.getMessage()); |
|
|
|
logger.info(e.getMessage()); |
|
|
|
|
|
|
|
} else if (e instanceof NoHandlerFoundException) { |
|
|
|
|
|
|
|
result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在"); |
|
|
|
|
|
|
|
} else if (e instanceof ServletException) { |
|
|
|
|
|
|
|
result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员"); |
|
|
|
String message; |
|
|
|
String message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", |
|
|
|
if (handler instanceof HandlerMethod) { |
|
|
|
|
|
|
|
HandlerMethod handlerMethod = (HandlerMethod) handler; |
|
|
|
|
|
|
|
message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", |
|
|
|
request.getRequestURI(), |
|
|
|
request.getRequestURI(), |
|
|
|
handlerMethod.getBean().getClass().getName(), |
|
|
|
handlerMethod.getBean().getClass().getName(), |
|
|
|
handlerMethod.getMethod().getName(), |
|
|
|
handlerMethod.getMethod().getName(), |
|
|
|
e.getMessage()); |
|
|
|
e.getMessage()); |
|
|
|
logger.error(message, e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (e instanceof NoHandlerFoundException) { |
|
|
|
|
|
|
|
result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在"); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(e.getMessage()); |
|
|
|
message = e.getMessage(); |
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(message); |
|
|
|
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
responseResult(response, result); |
|
|
|
responseResult(response, result); |
|
|
|
return new ModelAndView(); |
|
|
|
return new ModelAndView(); |
|
|
|