springboot全局異常處理代碼實(shí)例
這篇文章主要介紹了springboot全局異常處理代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
前言:
開發(fā)中異常的處理必不可少,常用的就是 throw 和 try catch,這樣一個(gè)項(xiàng)目到最后會(huì)出現(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ū)動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- alibaba的druid數(shù)據(jù)庫(kù)連接池 --> <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é)果類封裝:
這里簡(jiǎn)單封裝,實(shí)際根據(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 屬性: 指定我們需要攔截的注解,一個(gè)或多個(gè)(多個(gè)加到大括號(hào)中,逗號(hào)分隔) */ // @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) { // 打印異常信息到控制臺(tái) e.printStackTrace(); log.error("請(qǐng)求出現(xiàn)異常,異常信息為: {}", e.getMessage()); // 使用公共的結(jié)果類封裝返回結(jié)果, 這里我指定狀態(tài)碼為 400 return ApiResult.build(400, e.getMessage()); } }
4、異常測(cè)試類:
/** * 異常處理測(cè)試 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、測(cè)試:
5.1、請(qǐng)求接口:http://localhost:8080/exception/1 結(jié)果正常
5.2、請(qǐng)求接口:http://localhost:8080/exception/0 出現(xiàn)除以 0 錯(cuò)誤,全局異常處理起作用,返回指定結(jié)果集。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
idea項(xiàng)目中target文件提示拒絕訪問的解決
這篇文章主要介紹了idea項(xiàng)目中target文件提示拒絕訪問的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11IntelliJ IDEA 2017.1.4 x64配置步驟(介紹)
下面小編就為大家?guī)硪黄狪ntelliJ IDEA 2017.1.4 x64配置步驟(介紹)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06org.apache.ibatis.binding.BindingException異常報(bào)錯(cuò)原因以及詳細(xì)解決方案
這篇文章主要給大家介紹了關(guān)于org.apache.ibatis.binding.BindingException異常報(bào)錯(cuò)原因以及詳細(xì)解決方案的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02如何優(yōu)雅的拋出Spring Boot注解的異常詳解
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的拋出Spring Boot注解的異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12Springboot Apollo配置yml的問題及解決方案
這篇文章主要介紹了Springboot Apollo配置yml的問題及解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06