使用枚舉簡單封裝一個優雅的 Spring Boot 全局異常處理!

通過這篇文章,可以搞懂如何在 Spring Boot 中進行異常處理。但是,光是會用了還不行,我們還要思考如何把異常處理這部分的代碼寫的稍微優雅一點。下面我會以我在工作中學到的一點實際項目中異常處理的方式,來說說我覺得稍微優雅點的異常處理解決方案。

下面僅僅是我作為一個我個人的角度來看的,如果各位讀者有更好的解決方案或者覺得本文提出的方案還有優化的餘地的話,歡迎在評論區評論。

最終效果展示

下面先來展示一下完成後的效果,當我們定義的異常被系統捕捉後返回給客戶端的信息是這樣的:

使用枚舉簡單封裝一個優雅的 Spring Boot 全局異常處理!

返回的信息包含了異常下面 5 部分內容:

  1. 唯一標示異常的 code
  2. HTTP 狀態碼
  3. 錯誤路徑
  4. 發生錯誤的時間戳
  5. 錯誤的具體信息

這樣返回異常信息,更利於我們前端根據異常信息做出相應的表現。

異常處理核心代碼

ErrorCode.java (此枚舉類中包含了異常的唯一標識、HTTP 狀態碼以及錯誤信息)

這個類的主要作用就是統一管理系統中可能出現的異常,比較清晰明瞭。但是,可能出現的問題是當系統過於複雜,出現的異常過多之後,這個類會比較龐大。有一種解決辦法:將多種相似的異常統一為一個,比如將用戶找不到異常和訂單信息未找到的異常都統一為“未找到該資源”這一種異常,然後前端再對相應的情況做詳細處理(我個人的一種處理方法,不敢保證是比較好的一種做法)。

<code>import org.springframework.http.HttpStatus;


public enum ErrorCode {

RESOURCE_NOT_FOUND(1001, HttpStatus.NOT_FOUND, "未找到該資源"),
REQUEST_VALIDATION_FAILED(1002, HttpStatus.BAD_REQUEST, "請求數據格式驗證失敗");
private final int code;

private final HttpStatus status;

private final String message;

ErrorCode(int code, HttpStatus status, String message) {
this.code = code;
this.status = status;
this.message = message;
}

public int getCode() {
return code;
}

public HttpStatus getStatus() {
return status;
}

public String getMessage() {
return message;
}

@Override
public String toString() {
return "ErrorCode{" +
"code=" + code +
", status=" + status +
", message='" + message + '\\'' +
'}';
}
}/<code>

ErrorReponse.java(返回給客戶端具體的異常對象)

這個類作為異常信息返回給客戶端,裡面包括了當出現異常時我們想要返回給客戶端的所有信息。

<code>import org.springframework.util.ObjectUtils;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

public class ErrorReponse {
private int code;
private int status;
private String message;
private String path;
private Instant timestamp;
private HashMap<string> data = new HashMap<string>();

public ErrorReponse() {
}

public ErrorReponse(BaseException ex, String path) {
this(ex.getError().getCode(), ex.getError().getStatus().value(), ex.getError().getMessage(), path, ex.getData());
}

public ErrorReponse(int code, int status, String message, String path, Map<string> data) {
this.code = code;
this.status = status;
this.message = message;
this.path = path;
this.timestamp = Instant.now();
if (!ObjectUtils.isEmpty(data)) {
this.data.putAll(data);
}
}

// 省略 getter/setter 方法

@Override
public String toString() {
return "ErrorReponse{" +
"code=" + code +
", status=" + status +
", message='" + message + '\\'' +
", path='" + path + '\\'' +
", timestamp=" + timestamp +
", data=" + data +
'}';
}
}
/<string>/<string>/<string>/<code>

BaseException.java(繼承自 RuntimeException 的抽象類,可以看做系統中其他異常類的父類)

系統中的異常類都要繼承自這個類。

<code>public abstract class BaseException extends RuntimeException {
private final ErrorCode error;
private final HashMap<string> data = new HashMap<>();

public BaseException(ErrorCode error, Map<string> data) {
super(error.getMessage());
this.error = error;
if (!ObjectUtils.isEmpty(data)) {
this.data.putAll(data);
}
}

protected BaseException(ErrorCode error, Map<string> data, Throwable cause) {
super(error.getMessage(), cause);
this.error = error;
if (!ObjectUtils.isEmpty(data)) {
this.data.putAll(data);
}
}

public ErrorCode getError() {
return error;
}

public Map<string> getData() {
return data;
}

}/<string>/<string>/<string>/<string>/<code>

ResourceNotFoundException.java (自定義異常)

可以看出通過繼承 BaseException 類我們自定義異常會變的非常簡單!

<code>import java.util.Map;

public class ResourceNotFoundException extends BaseException {

public ResourceNotFoundException(Map<string> data) {
super(ErrorCode.RESOURCE_NOT_FOUND, data);
}
}/<string>/<code>

GlobalExceptionHandler.java(全局異常捕獲)

我們定義了兩個異常捕獲方法。

這裡再說明一下,實際上這個類只需要 handleAppException() 這一個方法就夠了,因為它是本系統所有異常的父類。只要是拋出了繼承 BaseException 類的異常後都會在這裡被處理。

<code>import com.twuc.webApp.web.ExceptionController;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

@ControllerAdvice(assignableTypes = {ExceptionController.class})
@ResponseBody
public class GlobalExceptionHandler {

// 也可以將 BaseException 換為 RuntimeException
// 因為 RuntimeException 是 BaseException 的父類
@ExceptionHandler(BaseException.class)
public ResponseEntity> handleAppException(BaseException ex, HttpServletRequest request) {
ErrorReponse representation = new ErrorReponse(ex, request.getRequestURI());
return new ResponseEntity<>(representation, new HttpHeaders(), ex.getError().getStatus());
}

@ExceptionHandler(value = ResourceNotFoundException.class)
public ResponseEntity<errorreponse> handleResourceNotFoundException(ResourceNotFoundException ex, HttpServletRequest request) {
ErrorReponse errorReponse = new ErrorReponse(ex, request.getRequestURI());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorReponse);
}

}
/<errorreponse>/<code>

(重要)一點擴展:

哈哈!實際上我多加了一個算是多餘的異常捕獲方法handleResourceNotFoundException() 主要是為了考考大家當我們拋出了 ResourceNotFoundException異常會被下面哪一個方法捕獲呢?

答案:

會被handleResourceNotFoundException()方法捕獲。因為 @ExceptionHandler 捕獲異常的過程中,會優先找到最匹配的。

下面通過源碼簡單分析一下:

ExceptionHandlerMethodResolver.java中getMappedMethod決定了具體被哪個方法處理。

<code>@Nullable
\tprivate Method getMappedMethod(Class extends Throwable> exceptionType) {
\t\tList<class>> matches = new ArrayList<>();
//找到可以處理的所有異常信息。mappedMethods 中存放了異常和處理異常的方法的對應關係
\t\tfor (Class extends Throwable> mappedException : this.mappedMethods.keySet()) {
\t\t\tif (mappedException.isAssignableFrom(exceptionType)) {
\t\t\t\tmatches.add(mappedException);
\t\t\t}
\t\t}
// 不為空說明有方法處理異常
\t\tif (!matches.isEmpty()) {
// 按照匹配程度從小到大排序
\t\t\tmatches.sort(new ExceptionDepthComparator(exceptionType));
// 返回處理異常的方法
\t\t\treturn this.mappedMethods.get(matches.get(0));

\t\t}
\t\telse {
\t\t\treturn null;
\t\t}
\t}/<class>/<code>

從源代碼看出:getMappedMethod()會首先找到可以匹配處理異常的所有方法信息,然後對其進行從小到大的排序,最後取最小的那一個匹配的方法(即匹配度最高的那個)。

寫一個拋出異常的類測試

Person.java

<code>public class Person {
private Long id;
private String name;

// 省略 getter/setter 方法
}/<code>

ExceptionController.java(拋出一場的類)

<code>@RestController
@RequestMapping("/api")
public class ExceptionController {

@GetMapping("/resourceNotFound")
public void throwException() {
Person p=new Person(1L,"SnailClimb");
throw new ResourceNotFoundException(ImmutableMap.of("person id:", p.getId()));
}

}/<code>


分享到:


相關文章: