溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Boot自定義返回錯(cuò)誤碼錯(cuò)誤信息的方法

發(fā)布時(shí)間:2021-02-03 12:30:52 來(lái)源:億速云 閱讀:509 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹了Spring Boot自定義返回錯(cuò)誤碼錯(cuò)誤信息的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

 說(shuō)明

?在實(shí)際的開(kāi)發(fā)過(guò)程中,很多時(shí)候要定義符合自己業(yè)務(wù)的錯(cuò)誤碼和錯(cuò)誤信息,而不是統(tǒng)一的而不是統(tǒng)一的下面這種格式返回到調(diào)用端

INTERNAL_SERVER_ERROR(500, "Internal Server Error"),

下面我們來(lái)看看如何將我們自定義的錯(cuò)誤碼和錯(cuò)誤信息返回到調(diào)用端。

1 自定義錯(cuò)誤碼

?首先我們要定義一個(gè)枚舉類

public enum ErrorEnum {
  /*
   * 錯(cuò)誤信息
   * */
  E_20011(20011, "缺少必填參數(shù)"),
  ;
  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 定義一個(gè)異常類

?定義一個(gè)異常類繼承RuntimeException類

public class BusinessException extends RuntimeException {
  private static final long serialVersionUID = 1L;
  private Integer code;
  /**
   * @param errorEnum 以錯(cuò)誤的ErrorEnum做參數(shù)
   */
  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 定義一個(gè)異常返回的模板類

?模板類定義了如何將異常通過(guò)什么形式進(jìn)行返回。

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

?通過(guò)postMan調(diào)用返回結(jié)果為

{ "message": "缺少必填參數(shù)", "code": 20011 }

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Spring Boot自定義返回錯(cuò)誤碼錯(cuò)誤信息的方法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI