Java SpringMVC 異常處理SimpleMappingExceptionResolver類詳解
Spring3.0 對(duì)異常的處理方式總共有兩種:
一種是使用 HandlerExceptionResolver 接口,并且 Spring 已經(jīng)提供默認(rèn)的實(shí)現(xiàn)類 SimpleMappingExceptionResolver。
第二種方法是在 Controller 內(nèi)部實(shí)現(xiàn),靈活性更高。
從目前的調(diào)查結(jié)果來看,這兩種方式不能共存。我們一般在項(xiàng)目中使用第一種方法。
下面分別描述一下這兩種使用方式:
一、基于 HandlerExceptionResolver 接口的方式
使用這種方式只需要實(shí)現(xiàn) resolveException 方法,該方法返回一個(gè) ModelAndView 對(duì)象,在方法內(nèi)部對(duì)異常的類型進(jìn)行判斷,然后返回合適的 ModelAndView 對(duì)象,如果該方法返回了 null,則 Spring 會(huì)繼續(xù)尋找其他的實(shí)現(xiàn)了 HandlerExceptionResolver 接口的 Bean。換句話說,Spring 會(huì)搜索所有注冊(cè)在其環(huán)境中的實(shí)現(xiàn)了 HandlerExceptionResolver 接口的 Bean,逐個(gè)執(zhí)行,直到返回了一個(gè) ModelAndView 對(duì)象。
public class CustomExceptionHandler implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) { if(exception instanceof IOException){ return new ModelAndView("ioexp"); }else if(exception instanceof SQLException){ return new ModelAndView("sqlexp"); } return null; } }
這個(gè)類必須聲明到 Spring 配置文件中,或者使用 @Component 標(biāo)簽,讓 Spring 管理它。同時(shí) Spring 也提供默認(rèn)的實(shí)現(xiàn)類 SimpleMappingExceptionResolver,需要使用時(shí)只需要使用注入到 Spring 配置文件進(jìn)行聲明即可。自定義實(shí)現(xiàn)類與默認(rèn)的實(shí)現(xiàn)類,可同時(shí)使用。
示例如下:
<!-- 自定義的實(shí)現(xiàn)類 --><bean id="exceptionHandler" class="com.enh.test.CustomExceptionHandler"/><!-- 默認(rèn)的實(shí)現(xiàn)類注入 --><bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!-- 為所有的異常定義默認(rèn)的異常處理頁(yè)面,exceptionMappings未定義的異常使用本默認(rèn)配置 --> <property name="defaultErrorView" value="error"></property> <!-- 定義異常處理頁(yè)面用來獲取異常信息的變量名,默認(rèn)名為exception --> <property name="exceptionAttribute" value="ex"></property> <!-- 定義需要特殊處理的異常,用類名或完全路徑名作為key,異常頁(yè)文件名作為值, 將不同的異常映射到不同的頁(yè)面上。 --> <property name="exceptionMappings"> <props> <prop key="IOException">error/ioexp</prop> <prop key="java.sql.SQLException">error/sqlexp</prop> </props> </property> </bean>
一個(gè)典型的異常顯示界面如下:
<html> <head><title>Exception!</title></head> <body> <% Exception ex = (Exception)request.getAttribute("exception"); %> <H2>Exception: <%= ex.getMessage();%></H2> <P/> <% ex.printStackTrace(new java.io.PrintWriter(out)); %> </body> </html>
exception 是在 SimpleMappingExceptionResolver 被存放到 request 中的,具體可以查看源代碼。
另外這里配置的異常顯示界面均僅包括主文件名,至于文件路徑和后綴已經(jīng)在 viewResolver 中指定。如果找不到頁(yè)面,會(huì)根據(jù)錯(cuò)誤提示再調(diào)整頁(yè)面路徑。
二、Controller 內(nèi)部單獨(dú)實(shí)現(xiàn)
該方法需要定義在 Controller 內(nèi)部,然后創(chuàng)建一個(gè)方法并用 @ExceptionHandler 注解來修飾用來處理異常,這個(gè)方法基本和 @RequestMapping 修飾的方法差不多,只是可以多一個(gè)類型為 Exception 的參數(shù),@ExceptionHandler 中可以添加一個(gè)或多個(gè)異常的類型,如果為空的話則認(rèn)為可以觸發(fā)所有的異常類型錯(cuò)誤。
@Controller public class ExceptionHandlerController { @ExceptionHandler(value={IOException.class,SQLException.class}) public String exp(Exception ex,HttpServletRequest request) { request.setAttribute("ex", ex); return "/error.jsp"; } }
三、相關(guān)問題
HandlerExceptionResolver 和 web.xml 中配置的 error-page 會(huì)有沖突嗎?
web.xml 中配置 error-page 同樣是配置出現(xiàn)錯(cuò)誤時(shí)顯示的頁(yè)面:
<error-page> <error-code>500</error-code> <location>/500.jsp</location> </error-page>
如果 resolveException 返回了 ModelAndView,會(huì)優(yōu)先根據(jù)返回值中的頁(yè)面來顯示。不過,resolveException 可以返回 null,此時(shí)則展示 web.xml 中的 error-page 的500狀態(tài)碼配置的頁(yè)面。
API 文檔中對(duì)返回值的解釋:
return a corresponding ModelAndView to forward to, or null for default processing.
到此這篇關(guān)于SpringMVC 異常處理SimpleMappingExceptionResolver類詳解的文章就介紹到這了,更多相關(guān)SpringMVC 異常處理SimpleMappingExceptionResolver類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java在指定路徑上創(chuàng)建文件提示不存在解決方法
在本篇文章里小編給大家整理的是一篇關(guān)于Java在指定路徑上創(chuàng)建文件提示不存在解決方法,有需要的朋友們可以參考下。2020-02-02Java實(shí)現(xiàn)動(dòng)態(tài)生成GIF圖像詳解
在互聯(lián)網(wǎng)上有許多有趣的場(chǎng)景,其中的一種就是動(dòng)圖。這不是視頻,而是一種GIF圖像信息。本文將利用Java實(shí)現(xiàn)動(dòng)態(tài)生成GIF圖像功能,需要的可以參考一下2022-09-09springboot整合sentinel接口熔斷的實(shí)現(xiàn)示例
為了防止慢接口導(dǎo)致的服務(wù)阻塞,可以通過添加熔斷處理來避免應(yīng)用的大量工作線程陷入阻塞,保證其他接口的正常運(yùn)行,本文介紹了如何使用Spring Boot與Sentinel進(jìn)行接口熔斷的配置與實(shí)現(xiàn),感興趣的可以了解一下2024-09-09SpringBoot將項(xiàng)目打成war包步驟解析
這篇文章主要介紹了SpringBoot將項(xiàng)目打成war包步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Java多線程 Callable、Future 和FutureTask
這篇文章主要介紹Java多線程中的 Callable、Future 以及FutureTask,下面文章圍繞Java多線程的相關(guān)資料展開全文詳細(xì)內(nèi)容,需要的朋友可以參考一下2021-10-10