全局异常处理方式
Spring Boot 针对全局异常,提供了如下三种处理方式。
实现 ErrorController
Spring Boot 将所有的错误默认映射到 /error 请求, 实现 ErrorController 接口,重写 getErrorPath 方法,跳转到错误页面。
@Controller
@RequestMapping(value = "error")
public class BaseErrorController implements ErrorController {
private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class);
@Override
public String getErrorPath() {
logger.info("出错啦!进入自定义错误控制器");
return "error/error";
}
@RequestMapping
public String error() {
return getErrorPath();
}
}
添加自定义错误页面
添加自定义错误页面,根据不同的错误情况跳转不同的错误提示页面。
- html 静态页面:在 resources/public/error/ 下定义,如添加 404 页面: resources/public/error/404.html 页面,中文注意页面编码;
- 模板引擎页面:在 templates/error/ 下定义,如添加 5xx 页面: templates/error/5xx.ftl;
注:templates/error/ 这个的优先级比较 resources/public/error/ 高。
基于注解 @ControllerAdvice
基于@ControllerAdvice 注解的全局异常统一处理只能针对于 Controller 层的异常,意思是只能捕获到 Controller 层的异常,在 Service 层或者其他层面的异常都不能捕获。
/**
* 全局异常处理
*
* @author yinjianwei
* @date 2018/07/30
*/
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 日志
*/
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 拦截捕捉业务异常
*
* @param ex
* @return
*/
@ExceptionHandler(value = ServiceException.class)
@ResponseBody
public DataResponse myErrorHandler(ServiceException e, HttpServletRequest request) {
String userIp = request.getRemoteAddr();
String requestURL = request.getRequestURL().toString();
String params = request.getQueryString();
Date date = new Date();
logger.error(String.format("Service Exception Log, time: %tF %tT|userIp: %s|requestURL: %s|params: %s", date,
date, userIp, requestURL, params), e);
return DataResponse.error(e.getErrorCode(), e.getMessage());
}
/**
* 全局异常捕捉处理
*
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public DataResponse errorHandler(Exception e, HttpServletRequest request) {
String userIp = request.getRemoteAddr();
String requestURL = request.getRequestURL().toString();
String params = request.getQueryString();
Date date = new Date();
logger.error(String.format("Exception Log, time:%tF %tT|userIp:%s|requestURL:%s|params:%s", date, date, userIp,
requestURL, params), e);
return DataResponse.error();
}
}
全局异常处理参考
@Controller
@RequestMapping(value = "error")
@EnableConfigurationProperties({ServerProperties.class})
public class ExceptionController implements ErrorController {
private ErrorAttributes errorAttributes;
@Autowired
private ServerProperties serverProperties;
/**
* 初始化ExceptionController
*
* @param errorAttributes
*/
@Autowired
public ExceptionController(ErrorAttributes errorAttributes) {
Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
this.errorAttributes = errorAttributes;
}
/**
* 定义404的ModelAndView
*
* @param request
* @param response
* @return
*/
@RequestMapping(produces = "text/html", value = "404")
public ModelAndView errorHtml404(HttpServletRequest request, HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));
return new ModelAndView("error/404", model);
}
/**
* 定义404的JSON数据
*
* @param request
* @return
*/
@RequestMapping(value = "404")
@ResponseBody
public ResponseEntity<Map<String, Object>> error404(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* 定义500的ModelAndView
*
* @param request
* @param response
* @return
*/
@RequestMapping(produces = "text/html", value = "500")
public ModelAndView errorHtml500(HttpServletRequest request, HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));
return new ModelAndView("error/500", model);
}
/**
* 定义500的错误JSON信息
*
* @param request
* @return
*/
@RequestMapping(value = "500")
@ResponseBody
public ResponseEntity<Map<String, Object>> error500(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* Determine if the stacktrace attribute should be included.
*
* @param request the source request
* @param produces the media type produced (or {@code MediaType.ALL})
* @return if the stacktrace attribute should be included
*/
protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) {
ErrorProperties.IncludeStacktrace include = this.serverProperties.getError().getIncludeStacktrace();
if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
return true;
}
if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
return getTraceParameter(request);
}
return false;
}
/**
* 获取错误的信息
*
* @param request
* @param includeStackTrace
* @return
*/
private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
}
/**
* 是否包含trace
*
* @param request
* @return
*/
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
/**
* 获取错误编码
*
* @param request
* @return
*/
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
try {
return HttpStatus.valueOf(statusCode);
} catch (Exception ex) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
/**
* 实现错误路径,暂时无用
*
* @see ExceptionMvcAutoConfiguration#containerCustomizer()
* @return
*/
@Override
public String getErrorPath() {
return "";
}
}