springboot全局異常處理代碼實例
這篇文章主要介紹了springboot全局異常處理代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
前言:
開發(fā)中異常的處理必不可少,常用的就是 throw 和 try catch,這樣一個項目到最后會出現(xiàn)很多冗余的代碼,使用全局異常處理避免過多冗余代碼。
全局異常處理:
1、pom 依賴(延續(xù)上一章代碼):
<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驅(qū)動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- alibaba的druid數(shù)據(jù)庫連接池 -->
<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、公共的結(jié)果類封裝:
這里簡單封裝,實際根據(jù)自己業(yè)務(wù)需求去封裝。
@Getter
@Setter
public class ApiResult {
// 響應(yīng)業(yè)務(wù)狀態(tài)
private Integer status;
// 響應(yīng)消息
private String msg;
// 響應(yīng)中的數(shù)據(jù)
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, msg, null);
}
public ApiResult(Integer status, String msg, 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、添加全局異常處理類(在入口函數(shù)下的包中新建):
/**
* 全局異常處理 Handler
* @ControllerAdvice 配置控制器通知
* annotations 屬性: 指定我們需要攔截的注解,一個或多個(多個加到大括號中,逗號分隔)
*/
// @RestControllerAdvice = @ResponseBody + @ControllerAdvice
@RestControllerAdvice(annotations = {RestController.class})
@Slf4j
public class GlobalExceptionHandler {
/**
* 默認(rèn)統(tǒng)一異常處理方法
* @ExceptionHandler 注解用來配置需要攔截的異常類型, 也可以是自定義異常
*/
@ExceptionHandler(Exception.class)
// 此處可以指定返回的狀態(tài)碼 和 返回 結(jié)果說明
// @ResponseStatus(reason = "exception",value = HttpStatus.BAD_REQUEST)
public Object runtimeExceptionHandler(Exception e) {
// 打印異常信息到控制臺
e.printStackTrace();
log.error("請求出現(xiàn)異常,異常信息為: {}", e.getMessage());
// 使用公共的結(jié)果類封裝返回結(jié)果, 這里我指定狀態(tài)碼為 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(">>>>>結(jié)果number為: {}", res);
return ApiResult.ok();
}
}
5、測試:
5.1、請求接口:http://localhost:8080/exception/1 結(jié)果正常
5.2、請求接口:http://localhost:8080/exception/0 出現(xiàn)除以 0 錯誤,全局異常處理起作用,返回指定結(jié)果集。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA 2017.1.4 x64配置步驟(介紹)
下面小編就為大家?guī)硪黄狪ntelliJ IDEA 2017.1.4 x64配置步驟(介紹)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
org.apache.ibatis.binding.BindingException異常報錯原因以及詳細(xì)解決方案
這篇文章主要給大家介紹了關(guān)于org.apache.ibatis.binding.BindingException異常報錯原因以及詳細(xì)解決方案的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
如何優(yōu)雅的拋出Spring Boot注解的異常詳解
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的拋出Spring Boot注解的異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Springboot Apollo配置yml的問題及解決方案
這篇文章主要介紹了Springboot Apollo配置yml的問題及解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06

