Spring的异常处理
一. 概述
问题:各个层都可能出现异常,异常代码写在哪一层?
解决:将所有异常都往上抛,最后在一个地方统一处理。-------------所有异常抛到表现层
问题:如果每个方法单独处理异常,代码工作量巨大;
解决:--------AOP思想 !Spring已经提供了异常处理器,不用自己写AOP;
二. 准备
2.1 表现层与前端数据协议
问题:前端这边收到的数据类型过多,需要使用统一的模型实体类进行封装;
解决:
- 将格式进行整合,统一格式;
- 创建一个模型实体类result,声明一个data属性;
- 添加一个code属性,用来区分增删改查操作;
- 当查到不存在的数据时,业务指定 code为20040 ,0结尾就是查询失败;
- 数据模型增加一个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 定义异常处理器
- 在controller层创建异常类添加注解**
@RestControllerAdive
**,代表异常处理器; - 在异常类中写方法处理异常,添加注解
@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)将可能出现的异常 包装为自定义异常并Throw抛出 !
将原本出现的异常在try…catch中包装成自定义的异常;
在业务层的BookServiceImpl中:
假如没有产生异常对象:则放入重载的另一个自定义异常类中不需要异常对象的构造器;
(3)异常处理器:拦截并分别处理不同类型的异常
- 异常被业务层中被 throw后,会被异常处理器拦截;
- 不同的方法拦截不同类型的异常,然后分别处理,再统一返回Result模型实体类给前端!