springboot全域性異常處理程式碼例項
阿新 • • 發佈:2020-01-07
這篇文章主要介紹了springboot全域性異常處理程式碼例項,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
前言:
開發中異常的處理必不可少,常用的就是 throw 和 try catch,這樣一個專案到最後會出現很多冗餘的程式碼,使用全域性異常處理避免過多冗餘程式碼。
全域性異常處理:
1、pom 依賴(延續上一章程式碼):
<dependencies> <!-- Spring Boot Web 依賴 --> <!-- Web 依賴 - 包含了 spring-boot-starter-validation 依賴--> <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> <!-- spring-boot整合mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- alibaba的druid資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency> <!--lombok依賴--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency> <!-- fastjson 依賴新增 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency> </dependencies>
2、公共的結果類封裝:
這裡簡單封裝,實際根據自己業務需求去封裝。
@Getter @Setter public class ApiResult { // 響應業務狀態 private Integer status; // 響應訊息 private String msg; // 響應中的資料 private Object data; public static ApiResult build(Integer status,String msg,Object data) { return new ApiResult(status,msg,data); } public static ApiResult ok(Object data) { return new ApiResult(data); } public static ApiResult ok() { return new ApiResult(null); } public ApiResult() { } public static ApiResult build(Integer status,String msg) { return new ApiResult(status,null); } public ApiResult(Integer status,Object data) { this.status = status; this.msg = msg; this.data = data; } public ApiResult(Object data) { this.status = 200; this.msg = "OK"; this.data = data; } }
3、新增全域性異常處理類(在入口函式下的包中新建):
/** * 全域性異常處理 Handler * @ControllerAdvice 配置控制器通知 * annotations 屬性: 指定我們需要攔截的註解,一個或多個(多個加到大括號中,逗號分隔) */ // @RestControllerAdvice = @ResponseBody + @ControllerAdvice @RestControllerAdvice(annotations = {RestController.class}) @Slf4j public class GlobalExceptionHandler { /** * 預設統一異常處理方法 * @ExceptionHandler 註解用來配置需要攔截的異常型別,也可以是自定義異常 */ @ExceptionHandler(Exception.class) // 此處可以指定返回的狀態碼 和 返回 結果說明 // @ResponseStatus(reason = "exception",value = HttpStatus.BAD_REQUEST) public Object runtimeExceptionHandler(Exception e) { // 列印異常資訊到控制檯 e.printStackTrace(); log.error("請求出現異常,異常資訊為: {}",e.getMessage()); // 使用公共的結果類封裝返回結果,這裡我指定狀態碼為 400 return ApiResult.build(400,e.getMessage()); } }
4、異常測試類:
/** * 異常處理測試 controller */ @RestController @Slf4j public class ExceptionController { @RequestMapping(value = "/exception/{number}") public ApiResult exception(@PathVariable int number) { int res = 10 / number; log.info(">>>>>結果number為: {}",res); return ApiResult.ok(); } }
5、測試:
5.1、請求介面:http://localhost:8080/exception/1 結果正常
5.2、請求介面:http://localhost:8080/exception/0 出現除以 0 錯誤,全域性異常處理起作用,返回指定結果集。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。