SpringMVC_异常统一处理

3.全局统一异常处理

3.1目前存在问题

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

    java 复制代码
    @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);
        }
  • 出现如下错误

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

  • 框架可能报错

  • 持久层代码报错

  • 业务层业务代码报错

  • 控制层业务代码报错

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

  • 解决方案

    java 复制代码
    @RestControllerAdvice
    public class ExceptionHandlerController {
    
        @ExceptionHandler(ArithmeticException.class)
        public ResultResp handlerException(){
            System.out.println("出现异常了");
            return new ResultResp(500,null,"服务繁忙,请稍后再试");
        }
    }

3.3@RestControllerAdvice

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

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

3.4@ExceptionHandler

  • 异常处理器

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

3.5项目中具体处理

  • 业务异常

    java 复制代码
    @Data
    public class BusinessException extends RuntimeException{
        private Integer code;
    }
  • 持久层异常

    java 复制代码
    @Data
    public class DaoException extends RuntimeException{
        private Integer code;
    }
  • 系统异常

    java 复制代码
    @Data
    public class SystemException extends RuntimeException{
        private Integer code;
    }
  • 其它异常

    java 复制代码
    @Data
    public class OtherException extends RuntimeException{
        private Integer code;
    }
  • 定义code

    java 复制代码
    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;
        
        
    
    }
  • 统一异常处理

    java 复制代码
    @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());
        }
    }
  • 控制层方法

    java 复制代码
     @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);
    }
相关推荐
それども12 分钟前
什么是MalformedStreamException,和WebKitFormBoundary有什么关系
java
思想在飞肢体在追38 分钟前
Springboot项目配置Nacos
java·spring boot·后端·nacos
cyforkk39 分钟前
09、Java 基础硬核复习:异常处理(容错机制)的核心逻辑与面试考点
java·数据库·面试
??(lxy)1 小时前
java高性能无锁队列——MpscLinkedQueue
java·开发语言
数研小生1 小时前
Full Analysis of Taobao Item Detail API taobao.item.get
java·服务器·前端
Wang15302 小时前
Java编程基础与面向对象核心概念
java
毕设源码-郭学长2 小时前
【开题答辩全过程】以 康复管理系统为例,包含答辩的问题和答案
java
毅炼2 小时前
hot100打卡——day17
java·数据结构·算法·leetcode·深度优先
winfreedoms2 小时前
java-网络编程——黑马程序员学习笔记
java·网络·学习
开开心心就好2 小时前
键盘改键工具免安装,自定义键位屏蔽误触
java·网络·windows·随机森林·计算机外设·电脑·excel