springboot自定义异常捕获

本文介绍了在系统中实现自定义响应与异常处理的方法。包括定义ResultCode接口用于操作状态,ResponseResult类用于封装响应结果,以及CommonCode枚举提供预定义的状态码。此外,还介绍了ExceptionCast类用于抛出自定义异常,以及ExceptionCatch类用于捕获并处理这些异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要的几个基本类:

ResultCode
Response
ResponseResult
CommonCode: 自定义code信息

/**
 * Created by 李新宇
 * 2019-07-31 17:54
 * <p>
 * 10000-- 通用错误代码
 * 22000-- 媒资错误代码
 * 23000-- 用户中心错误代码
 * 24000-- cms错误代码
 * 25000-- 文件系统
 */
public interface ResultCode {
    //操作是否成功,true为成功,false操作失败
    boolean success();
    //操作代码
    int code();
    //提示信息
    String message();

}
/**
 * Created by 李新宇
 * 2019-07-31 17:54
 */
public interface Response {
    public static final boolean SUCCESS = true;
    public static final int SUCCESS_CODE = 10000;
}
/**
 * Created by 李新宇
 * 2019-07-31 17:54
 */
@Data
@ToString
@NoArgsConstructor
public class ResponseResult implements Response {

    //操作是否成功
    boolean success = SUCCESS;

    //操作代码
    int code = SUCCESS_CODE;

    //提示信息
    String message;

    public ResponseResult(ResultCode resultCode) {
        this.success = resultCode.success();
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    public static ResponseResult SUCCESS() {
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public static ResponseResult FAIL() {
        return new ResponseResult(CommonCode.FAIL);
    }
}
/**
 * 自定义code信息
 * Created by 李新宇
 * 2019-07-31 17:54
 */
@ToString
public enum CommonCode implements ResultCode{

    SUCCESS(true,10000,"操作成功!"),
    FAIL(false,11111,"操作失败!"),
    UNAUTHENTICATED(false,10001,"此操作需要登陆系统!"),
    UNAUTHORISE(false,10002,"权限不足,无权操作!"),
    SERVER_ERROR(false,99999,"抱歉,系统繁忙,请稍后重试!");
//    private static ImmutableMap<Integer, CommonCode> codes ;
    //操作是否成功
    boolean success;
    //操作代码
    int code;
    //提示信息
    String message;
    private CommonCode(boolean success,int code, String message){
        this.success = success;
        this.code = code;
        this.message = message;
    }

    @Override
    public boolean success() {
        return success;
    }
    @Override
    public int code() {
        return code;
    }

    @Override
    public String message() {
        return message;
    }
}

自定义异常开始

ExceptionCast: 抛出自定义异常
ExceptionCatch: 统一异常捕获类

/**
 * 抛出自定义异常
 * <p>
 * Created by 李新宇
 * 2019-07-31 17:54
 */
public class ExceptionCast {

    //使用此静态方法抛出自定义异常
    public static void cast(ResultCode resultCode) {
        throw new CustomException(resultCode);
    }
}
/**
 * 统一异常捕获类
 * <p>
 * Created by 李新宇
 * 2019-07-31 17:58
 */
@ControllerAdvice //控制器增强
public class ExceptionCatch {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);

    //捕获CustomException此类异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseResult customException(CustomException customException) {
        //记录日志
        LOGGER.error("catch exception: {}", customException.getMessage());
        ResultCode resultCode = customException.getResultCode();
        return new ResponseResult(resultCode);
    }
}

使用

        if (cmsPage1 != null) {
            ExceptionCast.cast(CommonCode.FAIL);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值