Spring的异常处理

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Spring的异常处理

一. 概述

问题各个层都可能出现异常异常代码写在哪一层
解决将所有异常都往上抛最后在一个地方统一处理。-------------所有异常抛到表现层

问题如果每个方法单独处理异常代码工作量巨大
解决--------AOP思想 Spring已经提供了异常处理器不用自己写AOP

二. 准备

2.1 表现层与前端数据协议

问题前端这边收到的数据类型过多需要使用统一的模型实体类进行封装
解决

  1. 将格式进行整合统一格式
  2. 创建一个模型实体类result声明一个data属性
  3. 添加一个code属性用来区分增删改查操作
  4. 当查到不存在的数据时业务指定 code为20040 0结尾就是查询失败
  5. 数据模型增加一个message属性当查询失败时打印提示 “数据查询失败请重试”

2.1.1 数据实体类

public class Result {
    private Object data;
    private Integer code;
    private String msg;

    public Result() {
    }

    public Result(Integer code,Object data) {
        this.data = data;
        this.code = code;
    }

    public Result(Integer code, Object data, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

2.1.2 Code类业务指定

添加了异常相关的的Code

public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer GET_OK = 20041;

    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer GET_ERR = 20040;

    // 异常相关Code
    public static final Integer SYSTEM_ERR = 50001;  // 系统异常
    public static final Integer SYSTEM_TIMEOUT_ERR = 50002; 
    public static final Integer SYSTEM_UNKNOW_ERR = 59999; // 未知异常
    public static final Integer BUSINESS_ERR = 60002; // 业务异常
}

Controller中方法返回值变成Result模型实体类
在这里插入图片描述

2.2 定义异常处理器

  1. 在controller层创建异常类添加注解**@RestControllerAdive**代表异常处理器
  2. 在异常类中写方法处理异常添加注解 @ExeceptionHandler用于拦截异常把要拦截的异常种类的class写入 Exception.class即拦截所有的异常

在这里插入图片描述

注意异常类要被SpringMvc扫描到此时异常类在controller层中所以ComponenScan() 内有controller即可扫描到异常
在这里插入图片描述

三. 项目异常处理

在这里插入图片描述 在这里插入图片描述

1 自定义异常类

  • 新建一个exception层
  • 需要继承RuntimeException这种异常出现后可以不处理自动向上抛否则每次都要手动抛

SystemException

//自定义异常处理器用于封装异常信息对异常进行分类
public class BusinessException extends RuntimeException{
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

}

BusinessException

//自定义异常处理器用于封装异常信息对异常进行分类
public class BusinessException extends RuntimeException{
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

}

2将可能出现的异常包装转换为自定义的异常

将原本出现的异常在try…catch中包装成自定义的异常
业务层的BookServiceImpl中
在这里插入图片描述

假如没有产生异常对象则放入重载的另一个自定义异常类中不需要异常对象的构造器

3异常处理器拦截并分别处理不同类型的异常

  • 异常被业务层中被 throw后会被异常处理器拦截
  • 不同的方法拦截不同类型的异常然后分别处理再统一返回Result模型实体类给前端
    在这里插入图片描述
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring