SpringBoot自定义错误信息,SpringBoot自定义异常处理类,
SpringBoot异常结果处理适配页面及Ajax请求,
SpringBoot适配Ajax请求
================================
©Copyright 蕃薯耀 2018年3月29日
http://www.cnblogs.com/fanshuyao/
附件&源码下载见:http://fanshuyao.iteye.com/blog/2414865
一、自定义异常处理类:
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import com.lqy.springboot.component.CustomErrorAttribute;
- @ControllerAdvice
- public class CustomExceptionHandler {
- /**
- * 自定义异常数据
- * 缺点,没有适配页面和Ajax请求,返回的数据都是json数据
- * @param req
- * @return
- */
- /*@ResponseBody
- @ExceptionHandler(Exception.class)
- public Map<String, Object> exceptionHandler(HttpServletRequest req){
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("errorCode", 500);
- map.put("errorMsg", "错误信息");
- map.put("errorSystem", "errorSystem");
- return map;
- }*/
- /**
- * 自定义异常数据
- * 适配页面和Ajax请求
- * 注解ExceptionHandler(Exception.class)的Exception.class可以替换成自己定义的错误异常类
- * @param req
- * @return
- */
- @ExceptionHandler(Exception.class)
- public String exceptionHandler(HttpServletRequest req){
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("errorCode", 500);
- map.put("errorMsg", "错误信息");
- map.put("errorSystem", "errorSystem");
- req.setAttribute(CustomErrorAttribute.CUSTOM_ERROR_MAP_NAME, map);
- //传入自己的错误代码,必须的,否则不会进入自定义错误页面,见:org.springframework.boot.autoconfigure.web.AbstractErrorController
- req.setAttribute("javax.servlet.error.status_code", 500);
- //转发到springBoot错误处理请求,能适配网页和Ajax的错误处理
- //请求/error后,会进入BasicErrorController(@RequestMapping("${server.error.path:${error.path:/error}}"))
- //页面的数据显示处理是使用:errorAttributes.getErrorAttributes获取显示的,是AbstractErrorController的方法
- //当需要把自己定义的Map错误信息传递到错误提示页面时,
- //可以编写一个自定义错误属性类处理:CustomErrorAttribute,继承DefaultErrorAttributes类,
- //重写getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace)方法
- return "forward:/error";
- }
- }
异常捕捉后请求转发到
- return "forward:/error";
是为了让SpringBoot底层处理,协助系统适配页面返回结果和Ajax返回结果:当是页面打开时,会跳转到相应的错误页面显示异常信息,当是Ajax请求或者使用工具请求时,返回的json字符串。(下面有图)
SpringBoot适配页面返回结果和Ajax返回结果的代码如下:
- @RequestMapping(produces = "text/html")
- public ModelAndView errorHtml(HttpServletRequest request,
- HttpServletResponse response) {
- HttpStatus status = getStatus(request);
- Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
- request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
- response.setStatus(status.value());
- ModelAndView modelAndView = resolveErrorView(request, response, status, model);
- return (modelAndView == null ? new ModelAndView("error", model) : modelAndView);
- }
- @RequestMapping
- @ResponseBody
- public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
- Map<String, Object> body = getErrorAttributes(request,
- isIncludeStackTrace(request, MediaType.ALL));
- HttpStatus status = getStatus(request);
- return new ResponseEntity<Map<String, Object>>(body, status);
- }
二、自定义异常信息传递类
- import java.util.Map;
- import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.request.RequestAttributes;
- @Component
- public class CustomErrorAttribute extends DefaultErrorAttributes {
- public static final String CUSTOM_ERROR_MAP_NAME = "customErrorMap";
- @Override
- public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
- Map<String, Object> map = super.getErrorAttributes(requestAttributes, includeStackTrace);
- //设置传递自己定义的错误信息
- map.put(CUSTOM_ERROR_MAP_NAME, requestAttributes.getAttribute(CUSTOM_ERROR_MAP_NAME, RequestAttributes.SCOPE_REQUEST));
- return map;
- }
- }
定义这个类,是为了传递自己想要显示的错误信息,例如在Controller发生错误时,想把某些特殊信息传到错误页面,就可以自定义一个异常信息处理类,传递自己的自定义错误信息,同时也兼容SpringBoot本身定义好的错误 信息。
三、页面显示异常信息
- <!DOCTYPE html>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>500</title>
- </head>
- <body>
- <div>500错误</div>
- <div>path:[[${path}]]</div>
- <div>status:[[${status}]]</div>
- <div>timestamp:[[${#dates.format(timestamp, 'yyyy-MM-dd HH:mm:ss')}]]</div>
- <div>error:[[${error}]]</div>
- <div>exception:[[${exception}]]</div>
- <div>message:[[${message}]]</div>
- <div>errors:[[${errors}]]</div>
- <!-- 自定义属性 -->
- <div>customErrorMap.errorMsg:[[${customErrorMap.errorMsg}]]</div>
- <div>customErrorMap.errorSystem:[[${customErrorMap.errorSystem}]]</div>
- <div>customErrorMap.errorCode:[[${customErrorMap.errorCode}]]</div>
- </body>
- </html>
页面显示结果:
Post请求结果:
项目源码见附件:SpringBoot-自定义错误.zip
======SpringBoot自定义错误页面见:======
SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面
SpringBoot 4xx.html、5xx.html错误提示页面
http://www.cnblogs.com/fanshuyao/p/8668072.html
(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!)
================================
©Copyright 蕃薯耀 2018年3月29日
http://www.cnblogs.com/fanshuyao/