Spring Boot项目全局异常处理

2022-06-10 21:41:34 浏览数 (1)

全局异常处理的方式。

这个我之前做营销网站的时候就想搞了,那时异常处理有点乱,重复代码很多,想想我自己也有点问题,没有往这方面想,现在想想有点后悔,希望以后别再犯类似的错。

统一异常处理加起来倒是不麻烦,可以减少重复代码,让看来更简洁易懂。

下面的代码只是粗略写写,实际应用中需要进行适当修改,但是思路是一样的。

1、定义枚举类 ResultCodeEnum

代码语言:javascript复制
@Getter
public enum ResultCodeEnum {
    /**
     * 错误定义
     */
    SUCCESS(1000, "成功!"),
    BODY_NOT_MATCH(4000,"请求的数据格式不符!"),
    NOT_FOUND(4004, "未找到该资源!"),
    INTERNAL_SERVER_ERROR(5000, "服务器内部错误!"),
    SERVER_BUSY(5003,"服务器正忙,请稍后再试!"),
    SERVER_ERROR(9997,"业务异常");

    private final int resultCode;

    private final String resultMsg;

    private ResultCodeEnum(int resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }
}

这里只是粗略写一下。

2、自定义异常类 BizException

代码语言:javascript复制
@Data
public class BizException extends RuntimeException {

    private final Integer resultCode;
    private final String resultMsg;

    public BizException() {
        this.resultCode = ResultCodeEnum.SERVER_ERROR.getResultCode();
        this.resultMsg = ResultCodeEnum.SERVER_ERROR.getResultMsg();
    }

    public BizException(ResultCodeEnum resultCodeEnum) {
        this.resultCode = resultCodeEnum.getResultCode();
        this.resultMsg = resultCodeEnum.getResultMsg();
    }

    public BizException(ResultCodeEnum resultCodeEnum, String msg) {
        this.resultCode = resultCodeEnum.getResultCode();
        this.resultMsg = resultCodeEnum.getResultMsg();
    }

    public BizException(int resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }

}

3、定义返回类 BaseResponse

代码语言:javascript复制
@Data
public class BaseResponse {

    private Integer resultCode;
    private String resultMsg;


    public BaseResponse() {
        this.resultCode = ResultCodeEnum.SUCCESS.getResultCode();
        this.resultMsg = ResultCodeEnum.SUCCESS.getResultMsg();
    }

    public BaseResponse(ResultCodeEnum resultCodeEnum) {
        this.resultCode = resultCodeEnum.getResultCode();
        this.resultMsg = resultCodeEnum.getResultMsg();
    }

    public BaseResponse(ResultCodeEnum resultCodeEnum, String msg) {
        this.resultCode = resultCodeEnum.getResultCode();
        this.resultMsg = msg;
    }

    public BaseResponse(int resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }
}

4、定义全局异常处理类

切记要加注解 @RestControllerAdvice

代码语言:javascript复制
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    public GlobalExceptionHandler() {
    }

    @ExceptionHandler({Throwable.class})
    public BaseResponse handleThrowable(Throwable e) {
        log.error(e.getMessage(), e);
        return new BaseResponse(ResultCodeEnum.SERVER_ERROR.getResultCode(), e.getMessage());
    }

    @ExceptionHandler({Exception.class})
    public BaseResponse handleException(Exception e) {
        log.error(e.getMessage(), e);
        return new BaseResponse(ResultCodeEnum.SERVER_BUSY.getResultCode(), e.getMessage());
    }

    @ExceptionHandler(value =NullPointerException.class)
    public BaseResponse exceptionHandler(NullPointerException e){
        log.error(e.getMessage(), e);
        return new BaseResponse(ResultCodeEnum.BODY_NOT_MATCH);
    }

    @ExceptionHandler({BizException.class})
    public BaseResponse handleBizException(BizException e) {
        return new BaseResponse(e.getResultCode(), e.getResultMsg());
    }

    @ExceptionHandler({IOException.class})
    public BaseResponse handleIoException(IOException e) {
        log.error(e.getMessage(), e);
        return new BaseResponse(ResultCodeEnum.SERVER_ERROR.getResultCode(), "IO异常!");
    }

}

5、测试

代码语言:javascript复制
@Slf4j
@RestController
public class TestController {

    @PostMapping("test")
    public String test(@RequestBody User user) {
        if (user.getName() == null) {
            throw new BizException(4001, "name 参数不能为空");
        }

        return "success!";
    }
}

代码结构如下:

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links: https://lixj.fun/archives/springboot项目全局异常处理

0 人点赞