美文网首页技术分享
SpringBoot 错误处理机制

SpringBoot 错误处理机制

作者: 拉提娜的爸爸 | 来源:发表于2019-09-29 09:11 被阅读0次

1、如何定制错误的页面;

  • 有模板引擎的情况下
    将错误页面命名为错误状态码.html放在模板引擎文件夹里面的error文件夹下,当程序发生此状态码的错误就会来到对应的错误页面;
    我们可以使用4xx和5xx作为错误页面的文件名来匹配这种类型的所有错误,精确优先(优先寻找状态码.html,其次是4xx或5xx.html);
    错误页面能获取的信息:
    timestamp:时间戳
    status:状态码
    error:错误提示
    exception:异常对象
    message:异常消息
    errors:JSR303数据校验的错误都在这里
<h1>status:[[${status}]]</h1>
<p>error:[[${error}]]</p>
<p>exception:[[${exception}]]</p>
<p>message:[[${message}]]</p>
<p>errors:[[${errors}]]</p>
<p>timestamp:[[${#dates.format(timestamp,'yyyy-MM-dd HH:mm:ss')}]]</p>
  • 没有模板引擎(模板引擎找不到这个错误页面)
    静态资源文件夹下找相关的错误页面

    SpringBoot会在默认的静态资源文件路径下找错误页面,但这个页面不能动态获取错误信息
  • 以上都没有错误页面
    就是默认来到SpringBoot默认的错误提示页面

2、如何定制错误的json数据

  • 无论浏览器还是Postman等其他访问方式都会返回json格式的错误信息
/**
 * 异常处理器
 */
@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Map<String,Object> handleException(Exception e/*传入异常的信息*/){
        Map<String,Object> map = new HashMap<>();
        map.put("code",1);
        map.put("message",e.getMessage());
        return map;
    }
}
  • 浏览器进入定制的错误页面,Postman照常返回json格式
    @ExceptionHandler(MyException.class)
    public String handleException(Exception e/*传入异常的信息*/, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        System.out.println(e);
        // 传入自己的状态码
        request.setAttribute("javax.servlet.error.status_code",404);
        map.put("code",1);
        map.put("message",e.getMessage());
        return "forward:/error";
    }

主要操作是:传入自己的状态码request.setAttribute("javax.servlet.error.status_code",4xx);,如果没有这行代码,浏览器访问异常会自动跳到SpringBoot默认的错误页面

  • 定制数据加入到json里返回给客户端,并且浏览器响应为指定错误页面
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
        map.put("company","myErrorHandler"); // 可以给json返回值添加自己的字段和值

        // 将异常处理器的异常值保存在request里,然后在这里取出来保存起来
        Map<String, Object> resultMap = (Map<String, Object>) webRequest.getAttribute("result",0);
        map.put("result",resultMap);
        return map;
    }
}
/*--(分割线)---------------异常处理器MyExceptionHandler--------------*/
    @ExceptionHandler(MyException.class)
    public String handleException(Exception e/*传入异常的信息*/, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        System.out.println(e);
        // 传入自己的状态码
//        request.setAttribute("javax.servlet.error.status_code",404);
        map.put("code",1);
        map.put("message",e.getMessage());
        // 保存到request里
        request.setAttribute("result",map);
        return "forward:/error";
    }

页面上能用的数据,或者是json返回能用的数据都是通过errorAttributes.getErrorAttributes得到;,容器中默认使用DefaultErrorAttributes.getErrorAttributes()方法进行数据处理的;


返回结果

相关文章

网友评论

    本文标题:SpringBoot 错误处理机制

    本文链接:https://www.haomeiwen.com/subject/yjsmyctx.html