Springboot项目统一异常处理封装的两种方式

Springboot项目统一异常处理封装

方式一:通过枚举异常方式关联处理

一、异常枚举类继承关系
  1. RuntimeException 异常

    • BaseException 基础异常类
      • ArgumentException 参数异常类
      • BusinessException 业务异常类
      • ValidationException 校验异常类
  2. Assert 断言

    • CommonExceptionAssert 通用异常断言类
      • ArgumentResponseEnum 参数响应枚举
      • CommonResponseEnum 通用响应枚举
    • ArgumentExceptionAssert 参数异常断言
    • BusinessExceptionAssert 业务异常断言
      • RoomErrorEnum 房间异常枚举类
      • AccountErrorEnum 账户异常枚举类
  3. 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<?, ?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值