国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Spring Boot全局統一異常處理器

瀏覽:24日期:2023-07-15 09:11:34
目錄一、封裝統一返回結果類二、自定義異常封裝類三、錯誤枚舉四、全局異常處理類五、測試一、封裝統一返回結果類

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@Setterpublic class AjaxResult { //是否成功 private Boolean success; //狀態碼 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 類描述 */@Getter@Setterpublic class BusinessException extends RuntimeException{ private static final long serialVersionUID = 1L; /** * 錯誤狀態碼 */ 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 類描述 */public enum ErrorEnum { //數據操作錯誤定義 SUCCESS(200, '成功'), NO_PERMISSION(403, '你無權訪問'), NO_Auth(401, '未授權,請登錄驗證'), NO_FOUND(404, '未找到資源'), INTERNAL_SERVER_ERROR(500, '服務器異常, 請聯系管理員!'); /** * 錯誤碼 */ 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 類描述 */@RestControllerAdvicepublic 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 類描述 */@RestControllerAdvicepublic 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); }}

Spring Boot全局統一異常處理器

Spring Boot全局統一異常處理器

到此這篇關于Spring Boot全局統一異常處理器的文章就介紹到這了,更多相關Spring Boot異常處理器內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 高台县| 滦平县| 琼中| 铁力市| 蓝田县| 右玉县| 克东县| 伽师县| 容城县| 沧州市| 武山县| 阿坝| 贞丰县| 和硕县| 贡嘎县| 武威市| 舒城县| 武义县| 屯门区| 林口县| 定州市| 邳州市| 长丰县| 泽州县| 洛扎县| 柳林县| 西青区| 马关县| 宝山区| 三都| 濮阳县| 子长县| 颍上县| 阿鲁科尔沁旗| 阳信县| 磐安县| 炎陵县| 夏邑县| 盈江县| 阿城市| 阜新市|