Springboot项目统一异常处理封装
方式一:通过枚举异常方式关联处理
一、异常枚举类继承关系
-
RuntimeException 异常
- BaseException 基础异常类
- ArgumentException 参数异常类
- BusinessException 业务异常类
- ValidationException 校验异常类
- BaseException 基础异常类
-
Assert 断言
- CommonExceptionAssert 通用异常断言类
- ArgumentResponseEnum 参数响应枚举
- CommonResponseEnum 通用响应枚举
- ArgumentExceptionAssert 参数异常断言
- BusinessExceptionAssert 业务异常断言
- RoomErrorEnum 房间异常枚举类
- AccountErrorEnum 账户异常枚举类
- CommonExceptionAssert 通用异常断言类
-
GlobalExceptionAdvice 全局异常拦截处理类
二、代码实践整理
1、定义响应接口以及实现类
(1) 响应结果接口ResponseEnum
public interface ResponseEnum {
//返回码
int getCode();
//返回消息
String getMessage();
}
2、自定义异常基类以及实现类
(1)自定义异常基类BaseException
- 注意继承了-受检异常-RuntimeException
- 依赖了ResponseEnum
- 定义若干构造器
public class BaseException extends RuntimeException {
private static final long serialVersionUID = 1L;
protected ResponseEnum ResponseEnum;
protected Object[] args;
public BaseException(ResponseEnum ResponseEnum) {
super(ResponseEnum.getMessage());
this.ResponseEnum = ResponseEnum;
}
public BaseException(final int code, final String msg) {
super(msg);
this.ResponseEnum = new ResponseEnum() {
public int getCode() {
return code;
}
public String getMessage() {
return msg;
}
};
}
public BaseException(ResponseEnum ResponseEnum, Object[] args, String message) {
super(message);
this.ResponseEnum = ResponseEnum;
this.args = args;
}
public BaseException(ResponseEnum ResponseEnum, Object[] args, String message, Throwable cause) {
super(message, cause);
this.ResponseEnum = ResponseEnum;
this.args = args;
}
public ResponseEnum getResponseEnum() {
return this.ResponseEnum;
}
public Object[] getArgs() {
return this.args;
}
}
(2)自定义业务异常类BusinessException,
- 自定义异常类,可以用作不同的系统的标识,定义其他的异常类雷同
public class BusinessException extends BaseException {
private static final long serialVersionUID = 1L;
public BusinessException(ResponseEnum ResponseEnum, Object[] args, String message) {
super(ResponseEnum, args, message);
}
public BusinessException(ResponseEnum ResponseEnum, Object[] args, String message, Throwable cause) {
super(ResponseEnum, args, message, cause);
}
}
3、业务异常断言以及实现类
(1)异常断言基类Assert
- 定义各种异常处理的基础处理方法
public interface Assert {
BaseException newException(Object... var1);
BaseException newException(Throwable var1, Object... var2);
default void assertNotNull(Object obj) {
if (obj == null) throw this.newException();
}
default void assertNotNull(Object obj, Object... args) {
if (obj == null) throw this.newException(args);
}
default void assertNotEmpty(String str) {
if (null == str || "".equals(str.trim())) throw this.newException();
}
default void assertNotEmpty(String str, Object... args) {
if (StringUtils.isEmpty(str)) throw this.newException(args);
}
default void assertNotEmpty(Object[] arrays) {
if (StringUtils.isNotEmpty(str)) throw this.newException();
}
default void assertNotEmpty(Object[] arrays, Object... args) {
if (CollectionUtils.isEmpty(arrays)) throw this.newException(args);
}
default void assertNotEmpty(Collection<?> c) {
if (CollectionUtils.isEmpty(arrays)) throw this.newException();
}
default void assertNotEmpty(Collection<?> c, Object... args) {
if (CollectionUtils.isEmpty(arrays)) throw this.newException(args);
}
default void assertNotEmpty(Map<?, ?