• SpringMVC_异常统一处理


    3.全局统一异常处理

    3.1目前存在问题

    • 模拟后台出现服务器异常

      @GetMapping
          public ResultResp list(@RequestParam(required = false) String name){
              System.out.println(1/0);
              List<Item> ret = service.lists(name);
              return ResultResp.success(ret==null?Code.PAGE_FAIL:Code.PAGE_OK,ret);
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    • 出现如下错误

      在这里插入图片描述

    3.2后端服务可能会出现的异常

    • 框架可能报错

    • 持久层代码报错

    • 业务层业务代码报错

    • 控制层业务代码报错

    • 注意:这些异常不能避免的,此时应该把所有的异常在表现层进行统一的处理(aop)

    • 解决方案

      @RestControllerAdvice
      public class ExceptionHandlerController {
      
          @ExceptionHandler(ArithmeticException.class)
          public ResultResp handlerException(){
              System.out.println("出现异常了");
              return new ResultResp(500,null,"服务繁忙,请稍后再试");
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

    3.3@RestControllerAdvice

    • 使用来做控制器增强操作

      名称@RestControllerAdvice
      位置Rest 风格增强类上
      作用给控制器做增强操作
      使用类上面
      • 包含了如下注解
        • @ResponseBody
        • @ControllerAdvice
        • @Component

    3.4@ExceptionHandler

    • 异常处理器

      名称@ExceptionHandler
      位置方法上
      属性具体的异常类型
      作用处理具体异常的,设定具体异常类型,出现异常后,终止controller中方法的执行,转入当前方法执行

    3.5项目中具体处理

    • 业务异常

      @Data
      public class BusinessException extends RuntimeException{
          private Integer code;
      }
      
      • 1
      • 2
      • 3
      • 4
    • 持久层异常

      @Data
      public class DaoException extends RuntimeException{
          private Integer code;
      }
      
      • 1
      • 2
      • 3
      • 4
    • 系统异常

      @Data
      public class SystemException extends RuntimeException{
          private Integer code;
      }
      
      • 1
      • 2
      • 3
      • 4
    • 其它异常

      @Data
      public class OtherException extends RuntimeException{
          private Integer code;
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 定义code

      public class Code {
          /**
           * 定义好协议之后,前端和后端统一按照协议执行
           */
          public static final Integer SAVE_OK = 20000;
          public static final Integer SAVE_FAIL = 20001;
      
      
          public static final Integer UPDATE_OK = 20010;
          public static final Integer UPDATE_FAIL = 20011;
      
          public static final Integer DELETE_OK = 20020;
          public static final Integer DELETE_FAIL = 20021;
      
          public static final Integer GET_OK = 20030;
          public static final Integer GET_FAIL = 20031;
      
          public static final Integer PAGE_OK = 20040;
          public static final Integer PAGE_FAIL = 20041;
      
          
          public static final Integer BUSINESS_ERR = 50001;
          public static final Integer SYSTEM_ERR = 50002;
          public static final Integer DAO_ERR = 50003;
          public static final Integer OTHER_ERR = 50005;
          
          
      
      }
      
      • 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
    • 统一异常处理

      @RestControllerAdvice
      public class ExceptionHandlerController {
          //业务异常的例子:账户名和密码错误
          @ExceptionHandler(BusinessException.class)
          public ResultResp handlerBusinessException(BusinessException e){
              return new ResultResp(e.getCode(),null,e.getMessage());
          }
          
          //需要发送短信提醒运维人员
          @ExceptionHandler(SystemException.class)
          public ResultResp handlerSystemException(SystemException e){
              //发送短信提醒业务人员的操作
              //日志打印
              return new ResultResp(e.getCode(),null,e.getMessage());
          }
      
          @ExceptionHandler(OtherException.class)
          public ResultResp handlerException(OtherException e){
              return new ResultResp(e.getCode(),null,e.getMessage());
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
    • 控制层方法

       @GetMapping
      public ResultResp list(@RequestParam(required = false) String name) {
          if (name == null || name.equals(""))
              throw new BusinessException(Code.BUSINESS_ERR,"传参不正常请重试");
          List<Item> ret = null;
          try {
              ret = service.lists(name);
          } catch (Exception e) {
              throw new SystemException(Code.SYSTEM_ERR,"系统繁忙,请稍后再试");
          }
          return ResultResp.success(ret == null ? Code.PAGE_FAIL : Code.PAGE_OK, ret);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
  • 相关阅读:
    解决mysql8 Operation ALTER USER failed for ‘root’@’%‘
    .NET软件开发工程师面试题
    SpringCloud 三种服务调用方式,你学会了吗?
    2024年04月IDE流行度最新排名
    DCMM贯标是怎么评估等级的?
    FM5888协议系列-USB充电控制器 移动电源应用
    吃透底层:从路由到前缀树
    elementUI 中 date-picker 的使用的坑(vue3)
    Spring中自定义类型转换器
    与set和map相关的OJ题练习
  • 原文地址:https://blog.csdn.net/Byron__/article/details/132697686