springboot整合全局捕獲異常
一、前言
springboot整合全局捕獲異常。
在此記錄下,分享給大家。
二、springboot整合Mybatis
1、pom文件 依賴引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!-- SpringBoot 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SpringBoot web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 註解式 插入/構建/優雅代碼 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
2、 application.yml - 不配置即可
3、GlobalExceptionHandler.java
/**
* 全局異常捕獲
* Handler
* @author yys
*/
@ControllerAdvice
public class GlobalExceptionHandler {
// @ControllerAdvice 異常切面類
// @ResponseBody 如想返回視圖,去掉此註解即可
/**
* 全局運行時異常捕獲
* @return
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public Map<String, Object> exceptionHandler(Exception ex) {
HashMap<String, Object> map = new HashMap<String, Object>();
if(ex instanceof APIException) {
map.put("errorCode", ((APIException) ex).getErrorCode());
map.put("errorMsg", ((APIException) ex).getErrorMsg());
} else {
map.put("errorCode", 500);
map.put("errorMsg", "系統錯誤");
}
System.err.println("錯誤位置:[" + ex.getStackTrace()[0] + "]");
System.err.println("異常信息:[" + ex.getClass().getName() + "]");
System.err.println("錯誤信息:[" + ex.getMessage() + "]");
return map;
}
}
4、APIException.java
/**
* 自定義異常類
* Exception
* @author yys
*/
@Data
public class APIException extends Exception {
private static final long serialVersionUID = 1L;
/**
* errorCode:異常編碼
* errorMsg:異常描述
* resolvedMsg:解決方法描述
*/
private Integer errorCode;
private String errorMsg;
private String resolvedMsg;
public APIException(Integer errorCode) {
super("");
this.errorCode = errorCode;
this.errorMsg = "";
this.resolvedMsg = "";
}
public APIException(Integer errorCode, String errorMsg) {
super(errorMsg);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
this.resolvedMsg = "";
}
public APIException(Integer errorCode, String errorMsg, String resolvedMsg) {
super(errorMsg);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
this.resolvedMsg = resolvedMsg;
}
}
5、APIExceptionErrorCode.java
/**
* 自定義異常錯誤編碼
* Exception
* @author yys
*/
public class APIExceptionErrorCode {
/**
* 系統參數異常編碼
* 建議以1開頭
*/
public enum SysError {
APP_KEY_ERROR(10000, "APP_KEY錯誤,可能爲非法請求"),
SYS_PARAM_ERROR(10001, "系統參數不正確"),
TIME_EXPIRE_ERROR(10002, "系統時間差超過預設值,可能爲非法請求"),
ACCESS_TOKEN_ERROR(10003, "ACCESS_TOKEN錯誤,可能爲非法請求"),
ACCESS_TOKEN_EXPIRE(10004, "ACCESS_TOKEN已過期"),
SIGN_ERROR(10005, "簽名驗證不通過"),
NOT_EXIST(10006, "爲必填項"),
TRANSFORM_ERROR(10007, "類型轉換錯誤"),
SERVICE_ERROR(10008, "服務異常,請求不可達");
private int code;
private String message;
private SysError(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
/**
* 業務異常編碼
*/
public enum BizError {
/**
* 公共業務異常================================================
* 建議以9開頭
*/
APP_KEY_NOT_EXIST(90001,"APP_KEY不存在"),
API_TYPE_NULL(90002, "請求接口類型爲空"),
SPECIAL_CHARACTERS(90003, "上傳路徑存在特殊字符"),
BIZ_PARAM_ERROR(90004,"業務參數不正確"),
DATA_NOT_BELONG_PRV(90005, "訪問數據不屬於此當前賣家"),
FORM_DATA_IS_NULL(90006, "提交數據爲空"),
/**
* user業務異常================================================
* 建議以2開頭
*/
PROVIDER_NOT_EXIST(20001, "賣家不存在"),
/**
* prod業務異常================================================
* 建議以3開頭
*/
PROD_NOT_EXIST(30001, "商品不存在"),
/**
* order業務異常================================================
* 建議以4開頭
*/
ORDER_NOT_EXIST(40002, "訂單不存在");
private int code;
private String message;
private BizError(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
}
6、YysControler.java
/**
* 全局異常捕獲測試
* Controller
* @author yys
*/
@RestController
@RequestMapping("/test")
public class YysControler {
/**
* 系統異常測試
* @return
*/
@RequestMapping("/exception")
public String exception() {
int i = 1 / 0;
return "success";
}
/**
* 自定義異常測試
* @return
* @throws APIException
*/
@RequestMapping("/apiException")
public String apiException() throws APIException {
throw new APIException(APIExceptionErrorCode.SysError.SYS_PARAM_ERROR.getCode(),
APIExceptionErrorCode.SysError.SYS_PARAM_ERROR.getMessage());
}
}
7、啓動類
@SpringBootApplication
public class YysApp {
public static void main(String[] args) {
SpringApplication.run(YysApp.class, args);
}
}
8、測試
http://localhost:8080/test/exception
a、頁面結果 - 如下圖所示 :
b、控制檯結果 - 如下圖所示 :
http://localhost:8080/test/apiException
a、頁面結果 - 如下圖所示 :
b、控制檯結果 - 如下圖所示 :
Now ~ ~ ~寫到這裏,就寫完了,如果有幸幫助到你,請記得關注我,共同一起見證我們的成長。