SpringBoot業(yè)務邏輯異常的處理方法介紹
在Spring Boot項目中除了設置錯誤頁面,還可以通過注解實現(xiàn)錯誤處理。
局部異常
局部異常:
在控制器類中添加一個方法,結合@ExceptionHandler。但是只能對當前控制器中方法出現(xià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中設置異常處理
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í)行結果:
當訪問test的controller方法時,會出現(xiàn)除0異常,就會走異常處理方法,封裝異常信息,返回,錯誤類封裝的狀態(tài)信息。
全局異常
新建全局異常類,通過@ControllerAdvice結合@ExceptionHandler。當全局異常處理和局部處理同時存在時,局部生效(就近原則)
編寫全局異常處理器:
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; } }
測試結果如下:
我們再加上局部異常,再進行測試:
我們發(fā)現(xiàn)就近原則生效,異常被局部異常處理器捕獲處理
到此這篇關于SpringBoot業(yè)務邏輯異常的處理方法介紹的文章就介紹到這了,更多相關SpringBoot業(yè)務邏輯異常內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談Spring AOP中args()和argNames的含義
這篇文章主要介紹了Spring AOP中args()和argNames的含義,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java List的remove()方法陷阱以及性能優(yōu)化
Java List在進行remove()方法是通常容易踩坑,本文就詳細的介紹一下陷阱以及性能優(yōu)化,感興趣的可以了解一下2021-10-10BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式
這篇文章主要介紹了BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07springboot?ElasticSearch如何配置自定義轉換器ElasticsearchCustomConver
這篇文章主要介紹了springboot?ElasticSearch如何配置自定義轉換器ElasticsearchCustomConversions問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08