• SpringBoot里全局 非expcetion异常处理 非WebFlex


    SpringBoot异常处理机制
    在SpringBoot中,常用的异常处理有两种,一种是BasicErrorController,另一种是@ControllerAdvice,BasicErrorController用于处理非Controller抛出的异常,而@ControllerAdvice用于处理Controller抛出的异常,对于非Controller抛出的异常它是不会管的。

    但是,如果是Controller层调用Service层,从Service层抛出,依然会抛到Controller,所以还是会调用@ControllerAdvice进行处理

    以BasicErrorController为例,我们如何去创建一个自定的异常处理类呐?

    BasicErrorController创建及配置
    创建代码类并继承BasicErrorController
    代码类

    1. public class MyErrorController extends BasicErrorController {
    2. public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers) {
    3. super(errorAttributes, errorProperties, errorViewResolvers);
    4. }
    5. @SneakyThrows
    6. @Override
    7. protected Map getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
    8. Map attributes = super.getErrorAttributes(request, includeStackTrace);
    9. attributes.remove("timestamp");
    10. attributes.remove("status");
    11. attributes.remove("error");
    12. attributes.remove("exception");
    13. attributes.remove("path");
    14. String messageCode = (String) attributes.get("message");
    15. ErrorEnum errorEnum = null;
    16. try {
    17. errorEnum = ErrorEnum.getEnumByCode(messageCode);
    18. } catch (Exception e) {
    19. e.printStackTrace();
    20. }
    21. if (errorEnum==null){
    22. attributes.put("type","0");
    23. attributes.put("message","枚举类异常");
    24. return attributes;
    25. }
    26. attributes.put("errorControllerType","basicErrorController");
    27. attributes.put("type",errorEnum.getCode());
    28. attributes.put("message",errorEnum.getMessage());
    29. return attributes;
    30. }
    31. }

    配置MyErrorController
    首先我们知道SpringBoot的大部分配置可以在ErrorMvcAutoConfiguration中找到,打开ErrorMvcAutoConfiguration源码,可以看到ErrorMvcAutoConfiguration中对

    BasicErrorController进行了配置,为此在继承BasicErrorController后也应当对继承类进行配置。

    ErrorMvcAutoConfiguration中的BasicErrorController配置如下

    1. @Bean
    2. @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
    3. public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
    4. return new BasicErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers);
    5. }

    我们知道,需要ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers,三个参数,而ErrorMvcAutoConfiguration源码中配置中只有ErrorAttributes errorAttributes一个参数,而另外一个参数则在new BasicErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers) 中找到,即his.serverProperties.getError(),最后一个参数点两下,可以知道在ErrorMvcAutoConfiguration构造函数中,即this.errorViewResolvers = errorViewResolvers.orderedStream().collect(Collectors.toList());

    1. @Controller
    2. @RequestMapping({"${server.error.path:${error.path:/error}}"})
    3. public class CustomErrorController extends BasicErrorController {
    4. public CustomErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties,
    5. ObjectProvider> errorViewResolversProvider) {
    6. super(errorAttributes, serverProperties.getError(),
    7. (List)errorViewResolversProvider.orderedStream().collect(Collectors.toList()));
    8. }
    9. @RequestMapping
    10. public ResponseEntity> error(HttpServletRequest request) {
    11. HttpStatus status = super.getStatus(request);
    12. if (status == HttpStatus.NO_CONTENT) {
    13. return new ResponseEntity(status);
    14. } else {
    15. Map body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL));
    16. body.put("resp_msg", body.get("message"));
    17. body.put("resp_code", body.get("status"));
    18. body.remove("status");
    19. body.remove("message");
    20. return new ResponseEntity(body, status);
    21. }
    22. }
    23. }

  • 相关阅读:
    Java抽象类
    用“qwer”打造类似梦幻西游中比巧克力还丝滑的状态机
    观察者设计模式
    【uniapp uview】u--textarea组件custom validator check failed for prop “confirmType“
    前端知识以及组件学习总结
    IPV6的内容
    flutter 时间戳转日期
    docker安装ELK详细步骤(冒着被老板开的风险,生产试验,适用所有版本)
    面试面经|Java面试Redis面试题
    探索数据库的世界:DB、DBMS、DBA、DBS的全面介绍
  • 原文地址:https://blog.csdn.net/inthirties/article/details/126538708