我是靠谱客的博主 故意糖豆,这篇文章主要介绍Spring Boot学习系列(八)------错误处理机制,现在分享给大家,希望可以做个参考。

Spring Boot学习系列(八)------错误处理机制

前言

在日常的Web应用开发中,常常需要进行统一的错误出来返回给用户一个友好的页面,在SpringBoot中也有自己默认的错误处理机制,也可以基于此定制化我们自己的错误页面,下面来一起看一下!

正文

(一)SpringBoot默认的错误处理机制

如果我们访问不存在的页面或者服务器发生了错误,根据请求的不同,会返回不同的错误消息.

  1. 浏览器返回页面:

    在这里插入图片描述

  2. 如果是客户端请求,会返回如下个格式(这里我们使用postman):
    在这里插入图片描述

之所以返回的信息不一样,是因为浏览器和postman发送请求时,请求的头信息不一样,在Http请求的请求头中,有个头信息Accept,代表期望接收的数据类型,我们可以对比一 下:

浏览器:

在这里插入图片描述

postman:

在这里插入图片描述

正是因为请求头accept的不同,得到的数据也不一样,到底SpringBoot的底层做了哪些事,我们为什么会得到这个错误页面呢?我们知道,SpringBoot在启动的时候为我们自动配置了很多的组件,我们查看这些组件,可以看到有一个自动配置组件ErrorMvcAutoConfiguration,我们具体看看这个自动配置类,它主要包含了以下组件:

  • DefaultErrorAttributes
  • BasicErrorController
  • ErrorPageCustomizer
  • DefaultErrorViewResolver

其中,DefaultErrorAttributes定义在页面共享的错误信息:

复制代码
1
2
3
4
5
6
7
8
9
10
11
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { Map<String, Object> errorAttributes = new LinkedHashMap(); errorAttributes.put("timestamp", new Date()); this.addStatus(errorAttributes, webRequest); this.addErrorDetails(errorAttributes, webRequest, includeStackTrace); this.addPath(errorAttributes, webRequest); return errorAttributes; } //这个方法定义了页面错误消息显示的内容

BasicErrorController处理/error请求,代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Controller @RequestMapping({"${server.error.path:${error.path:/error}}"}) public class BasicErrorController extends AbstractErrorController { @RequestMapping(produces = {"text/html"}) //产生html格式的数据,如果是浏览器访问,由这个方法来处理请求 public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { HttpStatus status = this.getStatus(request); Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML))); response.setStatus(status.value()); //指定错误页面的地址和内容 ModelAndView modelAndView = this.resolveErrorView(request, response, status, model); return modelAndView != null ? modelAndView : new ModelAndView("error", model); } @RequestMapping @ResponseBody //产生json数据,其他客户端访问的时候由这个方法处理 public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { Map<String, Object> body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL)); HttpStatus status = this.getStatus(request); return new ResponseEntity(body, status); } }

ErrorPageCustomizer定制错误的处理规则

DefaultErrorViewResolver,

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) { ModelAndView modelAndView = this.resolve(String.valueOf(status), model); if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) { modelAndView = this.resolve((String)SERIES_VIEWS.get(status.series()), model); } return modelAndView; } private ModelAndView resolve(String viewName, Map<String, Object> model) { String errorViewName = "error/" + viewName; TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName, this.applicationContext); return provider != null ? new ModelAndView(errorViewName, model) : this.resolveResource(errorViewName, model); }

按照以上组件的代码,我们发现一旦系统发生4xx或者5xx类的错误,ErrorPageCustomizer就会根据配置的规则,请求/error,然后这个请求被BasicController处理.请求处理完成以后去哪个页面,由DefaultErrorViewResolver来决定

(二)定制自己的错误页面

  1. 使用了模板引擎:

    定义一个错误页面,命名为错误状态码.html,放在模板引擎文件夹下面的error文件夹下即可,如果发生了对应状态码的错误,就会来到这个页面,当然,你可以使用4xx.html或者5xx.html来匹配同一类的错误,精确优先.

  2. 没有使用模板引擎:

    直接在静态资源文件夹下创建error文件夹,然后创建对应的html页面放入即可.

根据上面对错误装配组件的研究,在错误页面可以获取以下几种错误信息:

  • timestamp 时间戳
  • status 状态码
  • error 错误提示
  • exception 异常对象
  • message 异常错误消息
  • error 数据校验错误返回的信息
  1. 使用注解定制异常信息

    注解处理异常有两种情况:

    • 局部异常处理: @Controller + @ExceptionHandler

      局部异常主要用的注解是@ExceptionHandler,使用时,将这个注解标注在方法上,注解中写明对应的异常,则发生这种异常时,此方法会被调用,如果@ExceptionHandler注解所在的类是用@Controller标注的,则该方法只对这个类有效.举例入如下:

      复制代码
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      @Controller public class IndexController { @RequestMapping({"/"}) public String login() { return "login"; } @RequestMapping("/ex") @ResponseBody public String testError() { int i = 5 / 0; return "ok"; } @ExceptionHandler(Exception.class) @ResponseBody public String handlerExceptionMethod(Exception e) { return "捕获到了除0异常"; } }

      访问页面时:

      在这里插入图片描述

    • 全局异常处理: @ControllerAdvice + @ExceptionHandler

      @ControllerAdvice注解是Spring3.2新增的,该注解可以定义@ExceptionHandler等注解,且作用到所有的@ReqeustMapping.也就是说,只有进入到Controller层的错误才会被@ControllerAdvice处理,拦截器爆出的错误以及访问错误地址的情况依旧由SpringBoot默认的处理机制来处理.下面来定义我们自己的全局异常处理.

      1. 首先我们自定义一个异常

        复制代码
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        public class MyException extends RuntimeException implements Serializable { private String msg; private int code; public MyException(String msg, int code) { this.msg = msg; this.code = code; } public MyException(String message, String msg, int code) { super(message); this.msg = msg; this.code = code; } public MyException(String message, Throwable cause, String msg, int code) { super(message, cause); this.msg = msg; this.code = code; } public MyException(Throwable cause, String msg, int code) { super(cause); this.msg = msg; this.code = code; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } }

        这里要注意,我们自定义的异常要继承RuntimeException,因为Spring对此类异常才会进行事务回滚

      2. 创建全局异常处理类

        复制代码
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        package com.xiaojian.springboot.international.exception; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; /** * 全局异常处理器 */ @ControllerAdvice //@RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) @ResponseBody public Object handlerException(Exception e, HttpServletRequest request) { //构建一个map System.out.println("开始制定异常处理>>>"); Map map = new HashMap<String, Object>(); if (e instanceof MyException) { map.put("msg", "发生了除0异常"); map.put("code", 100); }else { map.put("msg", "系统异常"); map.put("code", 500); } //使用HttpServletRequest判断请求是是否是ajax,返回不同的结果 String contentTypeHeader = request.getHeader("Content-Type"); String acceptHeader = request.getHeader("Accept"); String xRequestedWith = request.getHeader("X-Requested-With"); if ((contentTypeHeader != null && contentTypeHeader.contains("application/json")) || (acceptHeader != null && acceptHeader.contains("application/json")) || "XMLHttpRequest".equalsIgnoreCase(xRequestedWith)) { //在requet头信息中,如果X-Request-With是XMLHttpRequest,则代表请求是ajax异步请求 return map; }else { ModelAndView model = new ModelAndView(); model.addObject("msg", e.getMessage()); model.addObject("url", request.getRequestURL()); model.setViewName("error"); return model; } } }

        在这里,我们定义的GlobalExceptionHandler使用@ControllerAdvice标注的,因此进入controller层的错误都可以被处理器捕获到.在下面的方法上,我们使用@ExceptionHadelr标注,且定义处理的异常类为Exception.class,在项目运行的时候,如果发生了此类异常,就会执行这个方法.在方法中,根据是否是ajax请求,返回对应的model或者json数据. 在这里可以在类上使用@RestControllerAdvice,这样就不必在方法上使用@ResponseBody注解.

总结

SpringBoot中关于错误处理就写到这里,在实际的项目开发中,对错误处理会更全面一点,在以后的工作中,我也会不断的总结,持续进步!

最后

以上就是故意糖豆最近收集整理的关于Spring Boot学习系列(八)------错误处理机制的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(91)

评论列表共有 0 条评论

立即
投稿
返回
顶部