Spring Boot 如何自定义返回错误码错误信息

说明

  • 在实际的开发过程中,很多时候要定义符合自己业务的错误码和错误信息,而不是统一的而不是统一的下面这种格式返回到调用端
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),

下面我们来看看如何将我们自定义的错误码和错误信息返回到调用端。

1 自定义错误码

  • 首先我们要定义一个枚举类
public enum ErrorEnum {
    /*
     * 错误信息
     * */
    E_20011(20011, "缺少必填参数"),

    ;

    private Integer errorCode;
    private String errorMsg;
    ErrorEnum(Integer errorCode, String errorMsg) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }
    public Integer getErrorCode() {
        return errorCode;
    }
    public String getErrorMsg() {
        return errorMsg;
    }

2 定义一个异常类

  • 定义一个异常类继承RuntimeException类
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private Integer code;

    /**
     * @param errorEnum 以错误的ErrorEnum做参数
     */
    public BusinessException(ErrorEnum errorEnum) {
        super(errorEnum.getErrorMsg());
        this.code = errorEnum.getErrorCode();
        this.resultJson = CommonUtil.errorJson(errorEnum);
    }
    
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }



}

3 定义一个异常返回的模板类

  • 模板类定义了如何将异常通过什么形式进行返回。
public class ExceptionResponse {

    private String message;
    private Integer code;

    public ExceptionResponse(Integer code, String message) {
        this.message = message;
        this.code = code;
    }

    public static ExceptionResponse create(Integer code, String message) {
        return new ExceptionResponse(code, message);
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

}

4 定义全局处理 Controller 层异常

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ExceptionResponse handleException(Exception ex) {
        if (ex instanceof BusinessException) {
            log.warn(ex.getMessage(), ex);
            BusinessException businessException = (BusinessException) ex;
            return ExceptionResponse.create(businessException.getCode(), businessException.getMessage());
        } else {
            log.error(ex.getMessage(), ex);
            return ExceptionResponse.create(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
        }
    }

}

演示效果

  • 定义Controller层
@PostMapping("test/exception")
    public String testException() {
        throw new BusinessException(ErrorEnum.E_20011);
    }
  • 通过postMan调用返回结果为
    { "message": "缺少必填参数", "code": 20011 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 图片发自简书App title: 如何优雅的设计java异常date: 2017-03-31 17:36:36ca...
    luosv阅读 4,117评论 1 14
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,798评论 1 32
  • 阿里巴巴 JAVA 开发手册 1 / 32 Java 开发手册 版本号 制定团队 更新日期 备 注 1.0.0 阿...
    糖宝_阅读 12,266评论 0 5