• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

springboot 搞代码 4年前 (2022-01-09) 24次浏览 已收录 0个评论

 说明

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

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;
  }
  p<mark style="color:transparent">本文来源gaodaimacom搞#^代%!码网@</mark>ublic 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 ExceptionHandler {

  @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());
    }
  }
}

5 演示效果

•定义Controller层

@PostMapping("test/exception")
  public String testException() {
    throw new BusinessException(ErrorEnum.E_20011);
  }

•通过postMan调用返回结果为

{ "message": "缺少必填参数", "code": 20011 }

总结

以上所述是小编给大家介绍的Spring Boot 如何自定义返回错误码错误信息 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对搞代码网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring Boot 如何自定义返回错误码错误信息

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址