Spring Boot 如何自定義返回錯誤碼錯誤信息
說明
•在實(shí)際的開發(fā)過程中,很多時候要定義符合自己業(yè)務(wù)的錯誤碼和錯誤信息,而不是統(tǒng)一的而不是統(tǒng)一的下面這種格式返回到調(diào)用端
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
下面我們來看看如何將我們自定義的錯誤碼和錯誤信息返回到調(diào)用端。
1 自定義錯誤碼
•首先我們要定義一個枚舉類
public enum ErrorEnum {
/*
* 錯誤信息
* */
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 定義一個異常類
•定義一個異常類繼承RuntimeException類
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 1L;
private Integer code;
/**
* @param errorEnum 以錯誤的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 定義一個異常返回的模板類
•模板類定義了如何將異常通過什么形式進(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 如何自定義返回錯誤碼錯誤信息 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- 詳解SpringBoot基礎(chǔ)之banner玩法解析
- Spring Boot啟動banner定制的步驟詳解
- SpringBoot之Banner的使用示例
- Springboot2.0處理自定義異常并返回json
- Spring Boot 通過AOP和自定義注解實(shí)現(xiàn)權(quán)限控制的方法
- Spring Boot 自定義數(shù)據(jù)源DruidDataSource代碼
- springboot 自定義LocaleResolver實(shí)現(xiàn)切換語言
- Spring boot創(chuàng)建自定義starter的完整步驟
- Spring Boot自定義Banner實(shí)現(xiàn)代碼
相關(guān)文章
java后臺實(shí)現(xiàn)支付寶支付接口和支付寶訂單查詢接口(前端為APP)
這篇文章主要介紹了java后臺實(shí)現(xiàn)支付寶支付接口和支付寶訂單查詢接口(前端為APP),非常具有實(shí)用價值,需要的朋友可以參考下2018-08-08
Java實(shí)現(xiàn)經(jīng)典游戲打磚塊游戲的示例代碼
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)經(jīng)典的游戲—打磚塊。玩家操作一根螢?zāi)簧纤降摹鞍糇印?,讓一顆不斷彈來彈去的“球”在撞擊作為過關(guān)目標(biāo)消去的“磚塊”的途中不會落到螢?zāi)坏紫?。感興趣的小伙伴可以了解一下2022-02-02
java實(shí)現(xiàn)微博后臺登錄發(fā)送微博
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微博后臺登錄發(fā)送微博的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
關(guān)于SpringBoot禁止循環(huán)依賴解說
這篇文章主要介紹了關(guān)于SpringBoot禁止循環(huán)依賴解說,Spring的Bean管理,文章圍繞主題展開詳細(xì)介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05

