SpringBoot之异常处理

文章目录

  • 前言
  • 一、默认规则
  • 二、定制异常处理处理
    • 自定义错误页面
    • @ControllerAdvice+@ExceptionHandler处理全局异常
    • @ResponseStatus+自定义异常
    • 自定义实现 HandlerExceptionResolver 处理异常
  • 三、异常处理自动配置原理
  • 四、异常处理流程
  • 总结


前言

包含SpringBoot默认处理规则、如何定制错误(异常)处理逻辑、异常处理步骤流程、自定义处理代码。


一、默认规则

  • 默认情况下,Spring Boot提供/error处理所有错误的映射

  • 机器客户端,它将生成JSON响应,其中包含错误,HTTP状态和异常消息的详细信息。对于浏览器客户端,响应一个“ whitelabel”错误视图,以HTML格式呈现相同的数据:

{
  "timestamp": "2020-11-22T05:53:28.416+00:00",
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/asadada"
}

SpringBoot之异常处理_第1张图片

  • 要对其进行自定义,添加View解析为error

  • 要完全替换默认行为,可以实现 ErrorController 并注册该类型的Bean定义,或添加ErrorAttributes类型的组件以使用现有机制但替换其内容。

  • /templates/error/下的4xx,5xx页面会被自动解析,遇见4或5开头的错误会自动找到这俩页面
    SpringBoot之异常处理_第2张图片

二、定制异常处理处理

自定义错误页面

error/404.html error/5xx.html;有精确的错误状态码页面就匹配精确(如404就找404.html),没有就找 4xx.html;如果都没有就触发白页。

SpringBoot之异常处理_第3张图片

@ControllerAdvice+@ExceptionHandler处理全局异常

  • 底层是 ExceptionHandlerExceptionResolver 支持的。
package com.dragon.admin.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler({ArithmeticException.class, NullPointerException.class})//捕获的异常
    public String handlerArithmeticException(Exception e){
        log.error("异常是:{}",e);
        return "login";//捕获异常后返回的view
    }
}

@ResponseStatus+自定义异常

  • 底层是 ResponseStatusExceptionResolver ,把responsestatus注解的信息底层调用 response.sendError(statusCode, resolvedReason);tomcat发送的/error。
  • response.sendError(statusCode, resolvedReason) 方法相当于给Tomcat再次发送请求,请求为/error。且ResponseStatusExceptionResolver在调用完response.sendError()后会返回一个空view和空model的ModelAndView对象实例,以便结束本次请求的后续流程,直接进入/error请求处理逻辑。
  • 将状态码和错误原因组装成ModelAndView
package com.dragon.admin.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户数量太多")
public class UserTooManyException extends RuntimeException{
    public UserTooManyException(){}
    public UserTooManyException(String message){
        super(message);
    }
}

    @GetMapping("/cc")
    public String cc(Model model){
        //表格内容的遍历
        List<User> users = Arrays.asList(new User("zhangsan","123456"),
                new User("lisi","12344"),
                new User("haha","aaaaa"),
                new User("hehe","bbbb"));
        model.addAttribute("users",users);
        if(users.size()>3){
            throw new UserTooManyException();
        }
        return "table/aa";//这是在templates下有个table里面存放的aa.html
    }

大家只需要关注异常信息:用户数量太多。这个报错页面是我随便套用的,不要纠结状态码。
SpringBoot之异常处理_第4张图片

自定义实现 HandlerExceptionResolver 处理异常

可以作为默认的全局异常处理规则。
最终调用HttpServletResponseImpl的sendError方法。

@Order(value = Ordered.HIGHEST_PRECEDENCE)//设置优先级到最高
@Component
public class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        try {
            response.sendError(511,"我喜欢的错误");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new ModelAndView();
    }
}

三、异常处理自动配置原理

  • ErrorMvcAutoConfiguration 自动配置异常处理规则
    • 容器中的组件:类型:DefaultErrorAttributes -> id是errorAttributes
      • public class DefaultErrorAttributes implements ErrorAttributes, HandlerExceptionResolver
        
      • DefaultErrorAttributes:定义错误页面中可以包含哪些数据。
        
      SpringBoot之异常处理_第5张图片
    • 容器中的组件:类型:BasicErrorController --> id是basicErrorController(json+白页 适配响应)
      • 处理默认 /error 路径的请求;页面响应 new ModelAndView("error", model);
        
      • 容器中有组件 View->id是error;(响应默认错误页)
        
      • 容器中放组件 BeanNameViewResolver(视图解析器);按照返回的视图名作为组件的id去容器中找View对象。
        
    • 容器中的组件:类型:DefaultErrorViewResolver -> id是conventionErrorViewResolver
      • 如果发生错误,会以HTTP的状态码 作为视图页地址(viewName),找到真正的页面
      • error/404、5xx.html

四、异常处理流程

  • 执行目标方法,目标方法运行期间有任何异常都会被catch、而且标志当前请求结束;并且用 dispatchException 来保存catch到的异常
  • 进入视图解析流程(页面渲染) :processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);(有异常时,mv为空,异常保存在了dispatchException中)
  • mv = processHandlerException;处理handler发生的异常,处理完成返回ModelAndView;
    • 遍历所有的 handlerExceptionResolvers,看谁能处理当前异常【HandlerExceptionResolver处理器异常解析器】在这里插入图片描述

    • 系统默认的 异常解析器;SpringBoot之异常处理_第6张图片

      • DefaultErrorAttributes先来处理异常。把异常信息保存到request域,并且返回null;
      • 默认没有任何人能处理异常,所以异常会被抛出
        • 1.如果没有任何人能处理最终底层就会发送 /error 请求。会被底层的BasicErrorController处理
        • 2 .解析错误视图;遍历所有的 ErrorViewResolver 看谁能解析。
        • 3.默认的 DefaultErrorViewResolver ,作用是把响应状态码作为错误页的地址,error/500.html
        • 4.模板引擎最终响应这个页面 error/500.html (举个例子)
  • DefaultErrorAttribbutes只负责保存错误信息进request域,不能处理异常。
  • HandlerExceptionResolverComposite中的三个resolver默认无法处理异常,只有在特定情况下(如有相对应的异常注解等)才能发挥作用处理异常。

总结

以上就是SpringBoot关于异常处理的讲解。

你可能感兴趣的:(SpringBoot,spring,boot,后端,java,底层原理,异常处理)