SpringBoot全局異常處理機制和配置攔截器方式
SpringBoot全局異常處理機制和配置攔截器
關(guān)于SpringBoot的全局異常處理,其實十分簡單。
首先,要新建一個SpringBoot項目添加兩個依賴,spring-boot-starter-web和spring-boot-starter-thymeleaf。
這個時候,我們便可以把異常信息展示在thymeleaf頁面。
編寫接口:
以往的SpringMVC配置在SpringBoot項目中依然生效。
所以可以通過實現(xiàn)HandlerExceptionResolver接口或者添加@ExceptionHandler注解來處理全局異常。
@ControllerAdvice public class GlobalExceptionHandler implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView mv = new ModelAndView("01"); mv.addObject("error",ex.getMessage()); return mv; } }
@ControllerAdvice public class GlobalExceptionHandler2 { @ExceptionHandler(ArithmeticException.class) public ModelAndView resolveException( Exception ex) { ModelAndView mv = new ModelAndView("01"); mv.addObject("error",ex.getMessage()); return mv; } }
thymeleaf視圖必須放在/resources/templates文件夾下,此時需要新建一個01.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>測試頁面</title> </head> <body> <h4>異常信息顯示:</h4> <div th:text="${error}"></div> </body> </html>
注意:SpringBoot項目里面freemarker頁面和thymeleaf頁面不需要配置視圖解析器,Spring會自動幫我們識別并渲染ftlh和html頁面。
測試:
以上是SpringMVC配置在SpringBoot中依然適用。
但是基于SpringBoot項目的異常配置,SpringBoot自己也有一套全局異常處理機制。
SpringBoot異常處理
默認方案,在靜態(tài)頁面里面展示異常信息:
但是,必須要在靜態(tài)資源文件夾下新建error文件夾,里面放自己的錯誤頁面信息,4xx和5xx為模糊查詢,SpringBoot支持4xx和5xx的狀態(tài)錯誤。
這里為了省事,我就在頁面里放了一個div輸出一句話。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>4xx</title> </head> <body> <div>4xx-static-html</div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>5xx</title> </head> <body> <div>5xx-static-html</div> </body> </html>
測試結(jié)果為頁面顯示5xx-static-html,在這里還有一個小細節(jié),假如我們定義了一個精確的狀態(tài)碼頁面,例如數(shù)學算術(shù)錯誤為500,定義了一個500.html,那么是優(yōu)先顯示500的頁面還是5xx的頁面呢,這就涉及一個優(yōu)先級的問題。
在這里先埋下一個伏筆,繼續(xù)往下走。
我們知道thymeleaf實際上是一個動態(tài)頁面,所以同樣的,我們在templates文件下新建error文件夾放兩個html文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>4xx</title> </head> <body> <div>這是動態(tài)展示錯誤的頁面:</div> <table border="1"> <tr> <td>出錯路徑</td> <td th:text="${path}"></td> </tr> <tr> <td>出錯時間</td> <td th:text="${timestamp}"></td> </tr> <tr> <td>錯誤信息</td> <td th:text="${error}"></td> </tr> <tr> <td>狀態(tài)</td> <td th:text="${status}"></td> </tr> </table> </body> </html>
當我們在url地址請求一個不存在的頁面時觸發(fā),
測試:
好了,到這里要開始進入正題。
由于我們的錯誤是數(shù)學算術(shù)錯誤,返回的是java.lang.ArithmeticException,這樣顯然是很難交代的。
首先SpringBoot為我們額外提供了三個異常信息。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>5xx</title> </head> <body> <div>這是動態(tài)展示錯誤的頁面:</div> <table border="1"> <tr> <td>出錯路徑</td> <td th:text="${path}"></td> </tr> <tr> <td>出錯時間</td> <td th:text="${timestamp}"></td> </tr> <tr> <td>錯誤信息</td> <td th:text="${error}"></td> </tr> <tr> <td>狀態(tài)</td> <td th:text="${status}"></td> </tr> <tr> <td>異常</td> <td th:text="${exception}"></td> </tr> <tr> <td>堆棧</td> <td th:text="${trace}"></td> </tr> <tr> <td>異常信息</td> <td th:text="${message}"></td> </tr> </table> </body> </html>
exception、trace和message。
假如我們配置完5xx.html便急著去測試,結(jié)果如下:
這三個東西結(jié)果為空,那么小伙伴們肯定很想知道這三個東西究竟顯示的是什么玩意兒。
別急,我們可以在application.properties中配置三個東西。
server.error.include-exception=true server.error.include-message=always server.error.include-stacktrace=always
這三個是什么東西呢,想深究的同學可以回去看看底層。從字面意思理解,便是開啟這三個東西,默認為不展示。
測試:
原本是測試顯示的是java.lang.ArithmeticException,在這里只需要進行全局異常處理即可。
/** * 將該類注冊到 Spring 容器中,此時,默認的 DefaultErrorAttributes 就會失效 */ @Component public class MyErrorAttributes extends DefaultErrorAttributes { @Override public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { //這是默認的異常數(shù)據(jù) Map<String, Object> map = super.getErrorAttributes(webRequest,options); //獲取異常 String exception = (String) map.get("exception"); if ("java.lang.ArithmeticException".equals(exception)){ map.put("exception","數(shù)學算術(shù)異常"); } return map; } }
注意:狀態(tài)頁面先找精確,再找模糊,其次是先動態(tài)后靜態(tài)。
SpringBoot配置攔截器
SpringBoot的攔截器配置也是十分簡單,只需要把往Spring注冊一個攔截器即可。
為了方便演示,小編自定義一個攔截器,在控制臺打印了三句話。
隨后新建一個config包把該類注冊到Spring容器中。
大功告成,進入測試環(huán)節(jié):
這里打印了兩次的原因是,第一次攔截的是happy請求,第二次攔截的是404頁面錯誤,因此打印了兩次。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot 整合 Java DL4J 實現(xiàn)智能客服功能
本文主要介紹了如何使用SpringBoot整合JavaDeeplearning4j來構(gòu)建一個智能客服系統(tǒng),詳細探討了神經(jīng)網(wǎng)絡(luò)選擇、數(shù)據(jù)集格式、技術(shù)介紹、Maven依賴、代碼示例等內(nèi)容,為構(gòu)建高效、便捷、個性化的客戶服務(wù)提供了理論支持和實踐指導(dǎo)2024-10-10使用位運算、值交換等方式反轉(zhuǎn)java字符串的多種方法(四種方法)
這篇文章主要介紹了使用位運算、值交換等方式反轉(zhuǎn)java字符串,本文通過四種方式給大家講解,給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07完整的醫(yī)院就診掛號系統(tǒng)基于Spring MVC + Spring + MyBatis實現(xiàn)
這篇文章主要介紹了基于Spring MVC + Spring + MyBatis實現(xiàn)的醫(yī)院就診掛號系統(tǒng),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08java基礎(chǔ)之Integer與int類型輸出示例解析
這篇文章主要為大家介紹了java基礎(chǔ)之Integer與int類型輸出示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06