輕松搞定全局異常


SpringBoot 是為了簡化 Spring 應用的創建、運行、調試、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,我們只需遵循規范,引入相關的依賴就可以輕易的搭建出一個 WEB 工程

實際項目開發中,程序往往會發生各式各樣的異常情況,特別是身為服務端開發人員的我們,總是不停的編寫接口提供給前端調用,分工協作的情況下,避免不了異常的發生,如果直接將錯誤的信息直接暴露給用戶,這樣的體驗可想而知,且對黑客而言,詳細異常信息往往會提供非常大的幫助…

初窺異常

一個簡單的異常請求的接口

1
2
3
4
5
6
@GetMapping("/test1")
public String test1() {
// TODO 這里只是模擬異常,假設業務處理的時候出現錯誤了,或者空指針了等等...
int i = 10 / 0;
return "test1";
}

打開瀏覽器訪問它的時候發現

瀏覽器中的異常信息瀏覽器中的異常信息

又或者是用 postman 等模擬工具

postman 的異常信息postman 的異常信息

如果這接口是給第三方調用或者是自己公司的系統,看到這種錯誤估計得暴走吧….

笨方法(極其不建議)

采用try-catch的方式,手動捕獲異常信息,然后返回對應的結果集,相信很多人都看到過類似的代碼(如:封裝成Result對象);該方法雖然間接性的解決錯誤暴露的問題,同樣的弊端也很明顯,增加了大量的代碼量,當異常過多的情況下對應的catch層愈發的多了起來,很難管理這些業務異常和錯誤碼之間的匹配,所以最好的方法就是通過簡單配置全局掌控….

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@GetMapping("/test2")
public Map<String, String> test2() {
Map<String, String> result = new HashMap<>(16);
// TODO 直接捕獲所有代碼塊,然后在 cache
try {
int i = 10 / 0;
result.put("code", "200");
result.put("data", "具體返回的結果集");
} catch (Exception e) {
result.put("code", "500");
result.put("message", "請求錯誤");
}
return result;
}

具體代碼

通過上面的閱讀大家也大致能了解到為啥需要對異常進行全局捕獲了,接下來就看看 Spring Boot 提供的解決方案

導入依賴

在 pom.xml 中添加上 spring-boot-starter-web 的依賴即可

1
2
3
4
5
6
7
8
9
10
11
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

自定義異常

在應用開發過程中,除系統自身的異常外,不同業務場景中用到的異常也不一樣,為了與標題 輕松搞定全局異常 更加的貼切,定義個自己的異常,看看如何捕獲…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.battcn.exception;

/**
* 自定義異常
*
* @author Levin
* @since 2018/6/1 0001
*/
public class CustomException extends RuntimeException {

private static final long serialVersionUID = 4564124491192825748L;

private int code;

public CustomException() {
super();
}

public CustomException(int code, String message) {
super(message);
this.setCode(code);
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}
}

異常信息模板

定義返回的異常信息的格式,這樣異常信息風格更為統一

1
2
3
4
5
6
7
8
9
10
11
12
package com.battcn.exception;

/**
* @author Levin
* @since 2018/6/1 0001
*/
public class ErrorResponseEntity {

private int code;
private String message;
// 省略 get set
}

控制層

仔細一看是不是和平時正常寫的代碼沒啥區別,不要急,接着看….

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.battcn.controller;

import com.battcn.exception.CustomException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
* 全局異常演示
*
* @author Levin
* @since 2018/5/31 0031
*/
@RestController
public class ExceptionController {

@GetMapping("/test3")
public String test3(Integer num) {
// TODO 演示需要,實際上參數是否為空通過 @RequestParam(required = true) 就可以控制
if (num == null) {
throw new CustomException(400, "num不能為空");
}
int i = 10 / num;
return "result:" + i;
}
}

異常處理(關鍵)

注解概述

  • @ControllerAdvice 捕獲 Controller 層拋出的異常,如果添加 @ResponseBody 返回信息則為JSON 格式。
  • @RestControllerAdvice 相當於 @ControllerAdvice 與 @ResponseBody 的結合體。
  • @ExceptionHandler 統一處理一種類的異常,減少代碼重復率,降低復雜度。

創建一個 GlobalExceptionHandler 類,並添加上 @RestControllerAdvice 注解就可以定義出異常通知類了,然后在定義的方法中添加上 @ExceptionHandler 即可實現異常的捕捉…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.battcn.config;

import com.battcn.exception.CustomException;
import com.battcn.exception.ErrorResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 全局異常處理
*
* @author Levin
* @since 2018/6/1 0001
*/
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {


/**
* 定義要捕獲的異常 可以多個 @ExceptionHandler({})
*
* @param request request
* @param e exception
* @param response response
* @return 響應結果
*/
@ExceptionHandler(CustomException.class)
public ErrorResponseEntity customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
response.setStatus(HttpStatus.BAD_REQUEST.value());
CustomException exception = (CustomException) e;
return new ErrorResponseEntity(exception.getCode(), exception.getMessage());
}

/**
* 捕獲 RuntimeException 異常
* TODO 如果你覺得在一個 exceptionHandler 通過 if (e instanceof xxxException) 太麻煩
* TODO 那么你還可以自己寫多個不同的 exceptionHandler 處理不同異常
*
* @param request request
* @param e exception
* @param response response
* @return 響應結果
*/
@ExceptionHandler(RuntimeException.class)
public ErrorResponseEntity runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
response.setStatus(HttpStatus.BAD_REQUEST.value());
RuntimeException exception = (RuntimeException) e;
return new ErrorResponseEntity(400, exception.getMessage());
}

/**
* 通用的接口映射異常處理方
*/
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
HttpStatus status, WebRequest request) {
if (ex instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
return new ResponseEntity<>(new ErrorResponseEntity(status.value(), exception.getBindingResult().getAllErrors().get(0).getDefaultMessage()), status);
}
if (ex instanceof MethodArgumentTypeMismatchException) {
MethodArgumentTypeMismatchException exception = (MethodArgumentTypeMismatchException) ex;
logger.error("參數轉換失敗,方法:" + exception.getParameter().getMethod().getName() + ",參數:" + exception.getName()
+ ",信息:" + exception.getLocalizedMessage());
return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "參數轉換失敗"), status);
}
return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "參數轉換失敗"), status);
}
}

主函數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.battcn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author Levin
*/
@SpringBootApplication
public class Chapter17Application {

public static void main(String[] args) {
SpringApplication.run(Chapter17Application.class, args);
}

}

測試

完成准備事項后,啟動Chapter17Application,通過下面的測試結果可以發現,真的是 so easy,代碼變得整潔了,擴展性也變好了…

訪問 http://localhost:8080/test3

1
{"code":400,"message":"num不能為空"}

訪問 http://localhost:8080/test3?num=0

1
{"code":400,"message":"/ by zero"}

訪問 http://localhost:8080/test3?num=5

1
result:2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM