最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 如何自定義返回錯(cuò)誤碼錯(cuò)誤信息

 更新時(shí)間:2019年08月13日 10:18:58   作者:小魚兒_karl  
這篇文章主要介紹了Spring Boot 如何自定義返回錯(cuò)誤碼錯(cuò)誤信息的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

 說明

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

INTERNAL_SERVER_ERROR(500, "Internal Server Error"),

下面我們來看看如何將我們自定義的錯(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è)異常返回的模板類

•模板類定義了如何將異常通過什么形式進(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);
  }

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

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

總結(jié)

以上所述是小編給大家介紹的Spring Boot 如何自定義返回錯(cuò)誤碼錯(cuò)誤信息 ,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論

淳化县| 库尔勒市| 唐山市| 郎溪县| 政和县| 洮南市| 乐亭县| 大名县| 武邑县| 商都县| 珲春市| 灯塔市| 福贡县| 凤凰县| 五常市| 屯留县| 湄潭县| 安福县| 称多县| 马尔康县| 星子县| 华阴市| 绩溪县| 江口县| 宜兴市| 固始县| 易门县| 高碑店市| 松溪县| 石门县| 乌审旗| 聊城市| 北碚区| 灌阳县| 仁怀市| 晋宁县| 麦盖提县| 武邑县| 车险| 抚顺市| 阜新|