Spring Boot 如何自定義返回錯(cuò)誤碼錯(cuò)誤信息
說明
•在實(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)注明出處,謝謝!
- 詳解SpringBoot基礎(chǔ)之banner玩法解析
- Spring Boot啟動(dòng)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)文章
SpringBoot整合RocketMQ實(shí)現(xiàn)發(fā)送同步消息
RocketMQ 是一款開源的分布式消息中間件,由阿里巴巴開源,它具有高可用性、高性能、低延遲等特點(diǎn),廣泛應(yīng)用于阿里巴巴集團(tuán)內(nèi)部以及眾多外部企業(yè)的業(yè)務(wù)系統(tǒng)中,本文給大家介紹了SpringBoot整合RocketMQ實(shí)現(xiàn)發(fā)送同步消息,需要的朋友可以參考下2024-04-04
使用springboot 獲取控制器參數(shù)的幾種方法小結(jié)
這篇文章主要介紹了使用springboot 獲取控制器參數(shù)的幾種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java8中方便又實(shí)用的Map函數(shù)總結(jié)
java8之后,常用的Map接口中添加了一些非常實(shí)用的函數(shù),可以大大簡化一些特定場景的代碼編寫,提升代碼可讀性,快跟隨小編一起來看看吧2022-11-11
使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)
這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
分布式面試分布式鎖實(shí)現(xiàn)及應(yīng)用場景
這篇文章主要為大家介紹了關(guān)于分布式的面試問題,分布式鎖的實(shí)現(xiàn)及應(yīng)用不同場景下的使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03

