SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹
在Spring Boot項(xiàng)目中除了設(shè)置錯誤頁面,還可以通過注解實(shí)現(xiàn)錯誤處理。
局部異常
局部異常:
在控制器類中添加一個方法,結(jié)合@ExceptionHandler。但是只能對當(dāng)前控制器中方法出現(xiàn)異常進(jìn)行解決。
引入lombok依賴
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
1.創(chuàng)建異常信息類
package com.yyl.firstdemo.exception; import lombok.Data; @Data public class ExceptionMessage { private String code; private String message; }
2.在controller中設(shè)置異常處理
package com.yyl.firstdemo.controller; import com.yyl.firstdemo.exception.ExceptionMessage; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ErrorController { @RequestMapping("/test") public String testError(){ System.out.println(5/0); return "500"; } @ExceptionHandler(ArithmeticException.class) @ResponseBody public ExceptionMessage arithmeticException(Exception e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } }
@ExceptionHandler的參數(shù)為發(fā)生異常的類型。如果controller的方法中捕獲到了這種異常,就會走@ExceptionHandler表示的方法arithmeticException(),在方法參數(shù)中,可以獲取異常對象。
最終執(zhí)行結(jié)果:
當(dāng)訪問test的controller方法時,會出現(xiàn)除0異常,就會走異常處理方法,封裝異常信息,返回,錯誤類封裝的狀態(tài)信息。
全局異常
新建全局異常類,通過@ControllerAdvice結(jié)合@ExceptionHandler。當(dāng)全局異常處理和局部處理同時存在時,局部生效(就近原則)
編寫全局異常處理器:
package com.yyl.firstdemo.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class GlobalExceptionHandleController { //發(fā)生除0異常時,會執(zhí)行該方法 @ExceptionHandler(ArithmeticException.class) @ResponseBody public ExceptionMessage arithmeticException(Exception e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } //發(fā)生空指針異常時會執(zhí)行該方法 @ExceptionHandler(NullPointerException.class) @ResponseBody public ExceptionMessage nullPointerException(NullPointerException e){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage(e.getMessage()); return exceptionMessage; } //發(fā)生其他未知異常時,會執(zhí)行該方法 @ExceptionHandler(Exception.class) @ResponseBody public ExceptionMessage otherException(){ ExceptionMessage exceptionMessage = new ExceptionMessage(); exceptionMessage.setCode("500"); exceptionMessage.setMessage("發(fā)生了其他未知異常!"); return exceptionMessage; } }
編寫controller,增加兩個方法:
package com.yyl.firstdemo.controller; import com.yyl.firstdemo.exception.ExceptionMessage; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ErrorController { @RequestMapping("/test") public String testError(){ System.out.println(5/0); return "500"; } @RequestMapping("/testNull") public String testNull(){ String s = null; System.out.println(s.length()); return null; } @RequestMapping("testOther") public String testOther(){ int[] arr = new int[3]; System.out.println(arr[5]); return null; } }
測試結(jié)果如下:
我們再加上局部異常,再進(jìn)行測試:
我們發(fā)現(xiàn)就近原則生效,異常被局部異常處理器捕獲處理
到此這篇關(guān)于SpringBoot業(yè)務(wù)邏輯異常的處理方法介紹的文章就介紹到這了,更多相關(guān)SpringBoot業(yè)務(wù)邏輯異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring+Quartz配置定時任務(wù)實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring+Quartz配置定時任務(wù)實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04淺談Spring AOP中args()和argNames的含義
這篇文章主要介紹了Spring AOP中args()和argNames的含義,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java List的remove()方法陷阱以及性能優(yōu)化
Java List在進(jìn)行remove()方法是通常容易踩坑,本文就詳細(xì)的介紹一下陷阱以及性能優(yōu)化,感興趣的可以了解一下2021-10-10Java 信號量Semaphore的實(shí)現(xiàn)
這篇文章主要介紹了Java 信號量Semaphore的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式
這篇文章主要介紹了BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConver
這篇文章主要介紹了springboot?ElasticSearch如何配置自定義轉(zhuǎn)換器ElasticsearchCustomConversions問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08