Spring Boot全局統(tǒng)一異常處理器
一、封裝統(tǒng)一返回結(jié)果類
import com.jiusen.exercise.enums.ErrorEnum;
import com.jiusen.exercise.exception.BusinessException;
import lombok.Getter;
import lombok.Setter;
/**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 統(tǒng)一的返回結(jié)果
*/
@Getter
@Setter
public class AjaxResult {
//是否成功
private Boolean success;
//狀態(tài)碼
private Integer code;
//提示信息
private String msg;
//數(shù)據(jù)
private Object data;
public AjaxResult() {
}
//自定義返回結(jié)果的構(gòu)造方法
public AjaxResult(Boolean success, Integer code, String msg, Object data) {
this.success = success;
this.code = code;
this.msg = msg;
this.data = data;
}
//自定義異常返回的結(jié)果
public static AjaxResult defineError(BusinessException de) {
AjaxResult result = new AjaxResult();
result.setSuccess(false);
result.setCode(de.getErrorCode());
result.setMsg(de.getErrorMsg());
result.setData(null);
return result;
}
//其他異常處理方法返回的結(jié)果
public static AjaxResult otherError(ErrorEnum errorEnum){
AjaxResult result = new AjaxResult();
result.setMsg(errorEnum.getErrorMsg());
result.setCode(errorEnum.getErrorCode());
result.setSuccess(false);
result.setData(null);
return result;
}
}
二、自定義異常封裝類
import lombok.Getter;
import lombok.Setter;
/**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 類描述
*/
@Getter
@Setter
public class BusinessException extends RuntimeException{
private static final long serialVersionUID = 1L;
/**
* 錯(cuò)誤狀態(tài)碼
*/
protected Integer errorCode;
/**
* 錯(cuò)誤提示
*/
protected String errorMsg;
public BusinessException() {
}
public BusinessException(String message, Integer errorCode, String errorMsg) {
super(message);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
}
三、錯(cuò)誤枚舉
**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 類描述
*/
public enum ErrorEnum {
//數(shù)據(jù)操作錯(cuò)誤定義
SUCCESS(200, "成功"),
NO_PERMISSION(403, "你無權(quán)訪問"),
NO_Auth(401, "未授權(quán),請(qǐng)登錄驗(yàn)證"),
NO_FOUND(404, "未找到資源"),
INTERNAL_SERVER_ERROR(500, "服務(wù)器異常, 請(qǐng)聯(lián)系管理員!");
/**
* 錯(cuò)誤碼
*/
private Integer errorCode;
/**
* 錯(cuò)誤信息
*/
private String errorMsg;
ErrorEnum(Integer errorCode, String errorMsg) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public Integer getErrorCode() {
return errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
}
四、全局異常處理類
import com.jiusen.exercise.enums.ErrorEnum;
import com.jiusen.exercise.exception.BusinessException;
import com.jiusen.exercise.rest.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 類描述
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 處理自定義異常
* @param e
* @return
*/
@ExceptionHandler(value = BusinessException.class)
public AjaxResult bizExceptionHandler(BusinessException e) {
log.error(e.getErrorMsg(), e);
return AjaxResult.defineError(e);
}
/**
* 處理其它異常
*/
@ExceptionHandler(value = Exception.class)
public AjaxResult exceptionHandler(Exception e) {
log.error(e.getMessage(), e);
return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
}
}
五、測(cè)試
import com.jiusen.exercise.enums.ErrorEnum;
import com.jiusen.exercise.exception.BusinessException;
import com.jiusen.exercise.rest.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author: Lawson
* @date: 2021/5/11
* @description: TODO 類描述
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 處理自定義異常
* @param e
* @return
*/
@ExceptionHandler(value = BusinessException.class)
public AjaxResult bizExceptionHandler(BusinessException e) {
log.error(e.getErrorMsg(), e);
return AjaxResult.defineError(e);
}
/**
* 處理其它異常
*/
@ExceptionHandler(value = Exception.class)
public AjaxResult exceptionHandler(Exception e) {
log.error(e.getMessage(), e);
return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
}
}


到此這篇關(guān)于Spring Boot全局統(tǒng)一異常處理器的文章就介紹到這了,更多相關(guān)Spring Boot異常處理器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在java中 利用匿名內(nèi)部類進(jìn)行較簡(jiǎn)潔的雙括弧初始化的方法
本篇文章小編將為大家介紹,關(guān)于在java中 利用匿名內(nèi)部類進(jìn)行較簡(jiǎn)潔的雙括弧初始化的方法,有需要的朋友可以參考一下2013-04-04
Java使用Apache POI庫(kù)讀取Excel表格文檔的示例
POI庫(kù)是Apache提供的用于在Windows下讀寫各類微軟Office文檔的Java庫(kù),這里我們就來看一下Java使用Apache POI庫(kù)讀取Excel表格文檔的示例:2016-06-06
JUC系列學(xué)習(xí)工具類CountDownLatch詳解
這篇文章主要介紹了JUC系列學(xué)習(xí)工具類CountDownLatch詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可任意參考一下2022-08-08
spring boot實(shí)戰(zhàn)之使用JSP的示例
本篇文章主要介紹了spring boot實(shí)戰(zhàn)之使用JSP的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
idea已經(jīng)提交到遠(yuǎn)程分支,但需要本地和遠(yuǎn)程都回退到某一版本問題
這篇文章主要介紹了idea已經(jīng)提交到遠(yuǎn)程分支,但需要本地和遠(yuǎn)程都回退到某一版本問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java中valueOf和parseInt的區(qū)別詳解
這篇文章主要介紹了Java中valueOf和parseInt的區(qū)別詳解,在編程中,遇到類型轉(zhuǎn)換,好像會(huì)經(jīng)常用到 parseInt 和 valueOf,當(dāng)然這里只拿 Integer 類型進(jìn)行陳述,其他類型也是雷同的,需要的朋友可以參考下2024-01-01
Java?SpringBoot整合shiro-spring-boot-starterqi項(xiàng)目報(bào)錯(cuò)解決
這篇文章主要介紹了Java?SpringBoot整合shiro-spring-boot-starterqi項(xiàng)目報(bào)錯(cuò)解決,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考一下2022-08-08
關(guān)于mybatis callSettersOnNulls 配置解析
這篇文章主要介紹了關(guān)于mybatis callSettersOnNulls 配置,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-06-06

