本篇文章主要介紹的是SpringBoot項目進行全局異常的處理。html
說明:若是想直接獲取工程那麼能夠直接跳到底部,經過連接下載工程代碼。java
環境要求 JDK:1.8 SpringBoot:1.5.17.RELEASEgit
首先仍是Maven的相關依賴:github
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.17.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!-- Spring Boot Web 依賴 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
</dependencies>
複製代碼
配置文件這塊基本不須要更改,全局異常的處理只需在代碼中實現便可。web
SpringBoot的項目已經對有必定的異常處理了,可是對於咱們開發者而言可能就不太合適了,所以咱們須要對這些異常進行統一的捕獲並處理。SpringBoot中有一個ControllerAdvice
的註解,使用該註解表示開啓了全局異常的捕獲,咱們只需在自定義一個方法使用ExceptionHandler
註解而後定義捕獲異常的類型便可對這些捕獲的異常進行統一的處理。spring
咱們根據下面的這個示例來看該註解是如何使用吧。json
示例代碼:api
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(value =Exception.class)
public String exceptionHandler(Exception e){
System.out.println("未知異常!緣由是:"+e);
return e.getMessage();
}
}
複製代碼
上述的示例中,咱們對捕獲的異常進行簡單的二次處理,返回異常的信息,雖然這種可以讓咱們知道異常的緣由,可是在不少的狀況下來講,可能仍是不夠人性化,不符合咱們的要求。 那麼咱們這裏能夠經過自定義的異常類以及枚舉類來實現咱們想要的那種數據吧。springboot
自定義基礎接口類bash
首先定義一個基礎的接口類,自定義的錯誤描述枚舉類需實現該接口。 代碼以下:
public interface BaseErrorInfoInterface {
/** 錯誤碼*/
String getResultCode();
/** 錯誤描述*/
String getResultMsg();
}
複製代碼
自定義枚舉類
而後咱們這裏在自定義一個枚舉類,並實現該接口。 代碼以下:
public enum CommonEnum implements BaseErrorInfoInterface {
// 數據操做錯誤定義
SUCCESS("200", "成功!"),
BODY_NOT_MATCH("400","請求的數據格式不符!"),
SIGNATURE_NOT_MATCH("401","請求的數字簽名不匹配!"),
NOT_FOUND("404", "未找到該資源!"),
INTERNAL_SERVER_ERROR("500", "服務器內部錯誤!"),
SERVER_BUSY("503","服務器正忙,請稍後再試!")
;
/** 錯誤碼 */
private String resultCode;
/** 錯誤描述 */
private String resultMsg;
CommonEnum(String resultCode, String resultMsg) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
}
@Override
public String getResultCode() {
return resultCode;
}
@Override
public String getResultMsg() {
return resultMsg;
}
}
複製代碼
自定義異常類
而後咱們在來自定義一個異常類,用於處理咱們發生的業務異常。 代碼以下:
public class BizException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 錯誤碼
*/
protected String errorCode;
/**
* 錯誤信息
*/
protected String errorMsg;
public BizException() {
super();
}
public BizException(BaseErrorInfoInterface errorInfoInterface) {
super(errorInfoInterface.getResultCode());
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
}
public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
super(errorInfoInterface.getResultCode(), cause);
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
}
public BizException(String errorMsg) {
super(errorMsg);
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getMessage() {
return errorMsg;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
複製代碼
自定義數據格式
順便這裏咱們定義一下數據的傳輸格式。 代碼以下:
public class ResultBody {
/**
* 響應代碼
*/
private String code;
/**
* 響應消息
*/
private String message;
/**
* 響應結果
*/
private Object result;
public ResultBody() {
}
public ResultBody(BaseErrorInfoInterface errorInfo) {
this.code = errorInfo.getResultCode();
this.message = errorInfo.getResultMsg();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
/**
* 成功
*
* @return
*/
public static ResultBody success() {
return success(null);
}
/**
* 成功
* @param data
* @return
*/
public static ResultBody success(Object data) {
ResultBody rb = new ResultBody();
rb.setCode(CommonEnum.SUCCESS.getResultCode());
rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
rb.setResult(data);
return rb;
}
/**
* 失敗
*/
public static ResultBody error(BaseErrorInfoInterface errorInfo) {
ResultBody rb = new ResultBody();
rb.setCode(errorInfo.getResultCode());
rb.setMessage(errorInfo.getResultMsg());
rb.setResult(null);
return rb;
}
/**
* 失敗
*/
public static ResultBody error(String code, String message) {
ResultBody rb = new ResultBody();
rb.setCode(code);
rb.setMessage(message);
rb.setResult(null);
return rb;
}
/**
* 失敗
*/
public static ResultBody error( String message) {
ResultBody rb = new ResultBody();
rb.setCode("-1");
rb.setMessage(message);
rb.setResult(null);
return rb;
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
}
複製代碼
自定義全局異常處理類
最後咱們在來編寫一個自定義全局異常處理的類。 代碼以下:
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 處理自定義的業務異常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = BizException.class)
@ResponseBody
public ResultBody bizExceptionHandler(HttpServletRequest req, BizException e){
logger.error("發生業務異常!緣由是:{}",e.getErrorMsg());
return ResultBody.error(e.getErrorCode(),e.getErrorMsg());
}
/**
* 處理空指針的異常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =NullPointerException.class)
@ResponseBody
public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e){
logger.error("發生空指針異常!緣由是:",e);
return ResultBody.error(CommonEnum.BODY_NOT_MATCH);
}
/**
* 處理其餘異常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =Exception.class)
@ResponseBody
public ResultBody exceptionHandler(HttpServletRequest req, Exception e){
logger.error("未知異常!緣由是:",e);
return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
}
}
複製代碼
由於這裏咱們只是用於作全局異常處理的功能實現以及測試,因此這裏咱們只需在添加一個實體類和一個控制層類便可。
實體類
又是萬能的用戶表 (^▽^)
代碼以下:
public class User implements Serializable{
private static final long serialVersionUID = 1L;
/** 編號 */
private int id;
/** 姓名 */
private String name;
/** 年齡 */
private int age;
public User(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return JSONObject.toJSONString(this);
}
}
複製代碼
Controller 控制層
控制層這邊也比較簡單,使用Restful風格實現的CRUD功能,不一樣的是這裏我故意弄出了一些異常,好讓這些異常被捕獲到而後處理。這些異常中,有自定義的異常拋出,也有空指針的異常拋出,固然也有不可預知的異常拋出(這裏我用類型轉換異常代替),那麼咱們在完成代碼編寫以後,看看這些異常是否可以被捕獲處理成功吧!
代碼以下:
@RestController
@RequestMapping(value = "/api")
public class UserRestController {
@PostMapping("/user")
public boolean insert(@RequestBody User user) {
System.out.println("開始新增...");
//若是姓名爲空就手動拋出一個自定義的異常!
if(user.getName()==null){
throw new BizException("-1","用戶姓名不能爲空!");
}
return true;
}
@PutMapping("/user")
public boolean update(@RequestBody User user) {
System.out.println("開始更新...");
//這裏故意形成一個空指針的異常,而且不進行處理
String str=null;
str.equals("111");
return true;
}
@DeleteMapping("/user")
public boolean delete(@RequestBody User user) {
System.out.println("開始刪除...");
//這裏故意形成一個異常,而且不進行處理
Integer.parseInt("abc123");
return true;
}
@GetMapping("/user")
public List<User> findByUser(User user) {
System.out.println("開始查詢...");
List<User> userList =new ArrayList<>();
User user2=new User();
user2.setId(1L);
user2.setName("xuwujing");
user2.setAge(18);
userList.add(user2);
return userList;
}
}
複製代碼
App 入口
和普通的SpringBoot項目基本同樣。
代碼以下:
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println("程序正在運行...");
}
}
複製代碼
咱們成功啓動該程序以後,使用Postman工具來進行接口測試。
首先進行查詢,查看程序正常運行是否ok,使用GET 方式進行請求。
返回參數爲:
{"id":1,"name":"xuwujing","age":18}
示例圖:
而後咱們再來測試下自定義的異常是否可以被正確的捕獲並處理。
使用POST方式進行請求
Body參數爲:
{"id":1,"age":18}
返回參數爲:
{"code":"-1","message":"用戶姓名不能爲空!","result":null}
示例圖:
而後咱們再來測試下空指針異常是否可以被正確的捕獲並處理。在自定義全局異常中,咱們除了定義空指針的異常處理,也定義最高級別之一的Exception異常,那麼這裏發生了空指針異常以後,它是回優先使用哪個呢?這裏咱們來測試下。
使用PUT方式進行請求。
Body參數爲:
{"id":1,"age":18}
返回參數爲:
{"code":"400","message":"請求的數據格式不符!","result":null}
示例圖:
那麼咱們在來試試未指定其異常的處理,看該異常是否可以被捕獲。
使用DELETE方式進行請求。
Body參數爲:
{"id":1}
返回參數爲:
{"code":"500","message":"服務器內部錯誤!","result":null}
這裏能夠看到它使用了咱們在自定義全局異常處理類中的Exception異常處理的方法。 到這裏,測試就結束了。順便再說一下,自義定全局異常處理除了能夠處理上述的數據格式以外,也能夠處理頁面的跳轉,只需在新增的異常方法的返回處理上填寫該跳轉的路徑並不使用ResponseBody
註解便可。 細心的同窗也許發現了在GlobalExceptionHandler
類中使用的是ControllerAdvice
註解,而非RestControllerAdvice
註解,若是是用的RestControllerAdvice
註解,它會將數據自動轉換成JSON格式,這種於Controller
和RestController
相似,因此咱們在使用全局異常處理的以後能夠進行靈活的選擇處理。
關於SpringBoot優雅的全局異常處理的文章就講解到這裏了,若有不妥,歡迎指正!
SpringBoot全局異常的處理項目工程地址: github.com/xuwujing/sp…
SpringBoot整個集合的地址: github.com/xuwujing/sp…
原創不易,若是感受不錯,但願給個推薦!您的支持是我寫做的最大動力! 版權聲明: 做者:虛無境 博客園出處:www.cnblogs.com/xuwujing CSDN出處:blog.csdn.net/qazwsxpcm 我的博客出處:www.panchengming.com