- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Spring Boot全局統一異常處理器
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 統一的返回結果 */ @Getter @Setter public class AjaxResult { //是否成功 private Boolean success; //狀態(tài)碼 private Integer code; //提示信息 private String msg; //數據 private Object data; public AjaxResult() { } //自定義返回結果的構造方法 public AjaxResult(Boolean success, Integer code, String msg, Object data) { this.success = success; this.code = code; this.msg = msg; this.data = data; } //自定義異常返回的結果 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; } //其他異常處理方法返回的結果 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 類(lèi)描述 */ @Getter @Setter public class BusinessException extends RuntimeException{ private static final long serialVersionUID = 1L; /** * 錯誤狀態(tài)碼 */ protected Integer errorCode; /** * 錯誤提示 */ protected String errorMsg; public BusinessException() { } public BusinessException(String message, Integer errorCode, String errorMsg) { super(message); this.errorCode = errorCode; this.errorMsg = errorMsg; } }
** * @author: Lawson * @date: 2021/5/11 * @description: TODO 類(lèi)描述 */ public enum ErrorEnum { //數據操作錯誤定義 SUCCESS(200, "成功"), NO_PERMISSION(403, "你無(wú)權訪(fǎng)問(wèn)"), NO_Auth(401, "未授權,請登錄驗證"), NO_FOUND(404, "未找到資源"), INTERNAL_SERVER_ERROR(500, "服務(wù)器異常, 請聯(lián)系管理員!"); /** * 錯誤碼 */ 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; } }
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 類(lèi)描述 */ @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); } }
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 類(lèi)描述 */ @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全局統一異常處理器的文章就介紹到這了,更多相關(guān)Spring Boot異常處理器內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站