微信公众号:纯洁的麦田。如有问题,请后台留言,反正我也不会听。

前言

如题,今天介绍下 SpringBoot 是如何异常处理机制源码分析,自定义全局异常和自定义异常。

一、springboot原理探究

1.1、springboot默认错误处理机制

  • 错误页面
    9C30F57CA920400A97046082EAE205FE.png

  • 默认响应json数据
    image.png

1.2、源码分析

  • BasicErrorController.class 是默认处理/error请求
    image.png
    image.png

  • 类图
    image.png

  • 源码
    image.png

  • DefaultErrorAttributes.class异常处理类图
    image.png

1.3、总结

一般系统出现4xx或者5xx之类的错误;ErrorPageCustomizer就会生效(定制错误的响应规则);就会来到/error请求;就会被上面的
BasicErrorController处理。

二、自定义异常

2.1、异常返回页面

  • error.html页面 (templates/error.html)
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
<h1>错误信息界面</h1>
</body>
</html>
  • 自定义异常类:MyException
public class MyException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    public MyException(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private String code;
    private String msg;

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

}
  • 全局异常处理,页面或者json可调节 (可以加上你的逻辑,比如报错入库)
@RestControllerAdvice
public class MyControllerAdvice {

    private static final Logger LOG = LoggerFactory.getLogger(MyControllerAdvice.class);

    //捕获全局异常,处理所有不可知的异常
    @ExceptionHandler(value=Exception.class)
    //@ResponseBody
    public Object handleException(Exception e, HttpServletRequest request) {
        LOG.error("msg:{},url:{}", e.getMessage(), request.getRequestURL());

        Map<String, Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }

    //自定义异常
    //需要添加thymeleaf依赖
    //路径:src/main/resources/templates/error.html
    @ExceptionHandler(value=MyException.class)
    public Object handleMyException(MyException e, HttpServletRequest request) {
        //返回Json数据,由前端进行界面跳转
        //Map<String, Object> map = new HashMap<>();
        //map.put("code", e.getCode());
        //map.put("msg", e.getMsg());
        //map.put("url", request.getRequestURL());
        //return map;

        //进行页面跳转
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/error.html");
        modelAndView.addObject("msg", e.getMsg());
        return modelAndView;
    }
}
  • 接口请求
@RestController
@RequestMapping("/exce")
public class ExceptionController {

	// 报错异常
    @RequestMapping("/api/v1/exce")
    public Object testException() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "ytheng");
        map.put("pwd", 123456);

        int data = 1/0;
        return map;
    }

	// 自定义异常
    @RequestMapping("/api/v1/myexce")
    public Object testMyException() {
        throw new MyException("500", "my ext异常");
    }
}

报错异常
image.png
/自定义异常
image.png

后语

如果本文对你哪怕有一丁点帮助,请帮忙点好看。你的好看是我坚持写作的动力。
另外,关注:纯洁的麦田,一起学习吧。