SpringBoot異常處理的原理分析

這篇文章主要介紹“SpringBoot異常處理的原理分析”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“SpringBoot異常處理的原理分析”文章能幫助大家解決問題。

我們擁有10余年網(wǎng)頁設(shè)計和網(wǎng)站建設(shè)經(jīng)驗,從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計師為您提供的解決方案。為企業(yè)提供成都網(wǎng)站制作、網(wǎng)站設(shè)計、微信開發(fā)、微信小程序開發(fā)、手機網(wǎng)站制作設(shè)計html5、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計或者設(shè)計方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計服務(wù)并滿足您的需求。

異常處理流程

執(zhí)行目標(biāo)方法,目標(biāo)方法運行期間有任何異常都會被catch捕獲,并標(biāo)志當(dāng)前請求結(jié)束,dispatchException拋出異常

SpringBoot異常處理的原理分析

進入視圖解析流程,并渲染頁面,發(fā)生異常時,參數(shù)mv為空,傳入捕獲的異常dispatchException

SpringBoot異常處理的原理分析

處理handler發(fā)生的異常,處理完成返回ModelAndView

SpringBoot異常處理的原理分析

(1)遍歷所有的HandlerExceptionResolvers,找到可以處理當(dāng)前異常的解析器來解析異常

SpringBoot異常處理的原理分析

(2)調(diào)用resolveException解析異常,傳入requestresponse對象,哪個方法,發(fā)生的異常,然后自定義異常處理返回ModelAndView

SpringBoot異常處理的原理分析

(3)系統(tǒng)默認(rèn)的異常解析器

SpringBoot異常處理的原理分析

① DefaultErrorAttributes先來處理異常,把異常信息保存到request域并返回null

SpringBoot異常處理的原理分析

② ExceptionHandlerExceptionResolver用來處理標(biāo)注了@ExceptionHandler注解的方法異常

③ ResponseStatusExceptionResolver用來處理標(biāo)注了@ResponseStatus注解的方法異常

④ DefaultHandlerExceptionResolver默認(rèn)的處理器異常解析器,處理一些常見的異常

(4)如果沒有任何解析器能夠處理異常,異常就會拋出

SpringBoot異常處理的原理分析

(5)如果沒有任何解析器能夠處理當(dāng)前異常,最終就會發(fā)送/error請求,將保存的異常信息轉(zhuǎn)發(fā)到/error。BasicErrorController專門來處理/error請求,BasicErrorController會遍歷所有的ErrorViewResolver解析錯誤視圖,如果沒有自定義的錯誤視圖解析器,就會使用默認(rèn)的DefaultErrorViewResolver,會把響應(yīng)碼作為錯誤頁的地址,模板引擎最終響應(yīng)這個頁面。

幾種異常處理方式及原理

1.自定義錯誤頁,error/404.htmlerror/5xx.html。有精確的錯誤狀態(tài)碼頁面就匹配精確,沒有就找 4xx.html,如果都沒有就觸發(fā)白頁

2.使用@ControllerAdvice@ExceptionHandler處理全局異常,底層是ExceptionHandlerExceptionResolver 支持的

SpringBoot異常處理的原理分析

3.使用@ResponseStatus和自定義異常。底層是 ResponseStatusExceptionResolver ,底層調(diào)用 response.sendError(statusCode, resolvedReason),Tomcat會收到一個error。請求最后new一個空的ModelAndView返回,這樣任何處理解析器都處理不了當(dāng)前的異常,最終就會發(fā)送/error請求,BasicErrorController專門來處理/error請求,適配4xx.html或者5xx.html頁面

SpringBoot異常處理的原理分析

4.Spring底層的異常,如參數(shù)類型轉(zhuǎn)換異常。底層是DefaultHandlerExceptionResolver 處理框架底層的異常,底層也是response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE),Tomcat會收到一個error。請求最后new一個空的ModelAndView返回,這樣任何處理解析器都處理不了當(dāng)前的異常,最終就會發(fā)送/error請求,BasicErrorController專門來處理/error請求,適配4xx.html或者5xx.html頁面

protected ModelAndView doResolveException(
			HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {

		try {
			if (ex instanceof HttpRequestMethodNotSupportedException) {
				return handleHttpRequestMethodNotSupported(
						(HttpRequestMethodNotSupportedException) ex, request, response, handler);
			}
			else if (ex instanceof HttpMediaTypeNotSupportedException) {
				return handleHttpMediaTypeNotSupported(
						(HttpMediaTypeNotSupportedException) ex, request, response, handler);
			}
			else if (ex instanceof HttpMediaTypeNotAcceptableException) {
				return handleHttpMediaTypeNotAcceptable(
						(HttpMediaTypeNotAcceptableException) ex, request, response, handler);
			}
			else if (ex instanceof MissingPathVariableException) {
				return handleMissingPathVariable(
						(MissingPathVariableException) ex, request, response, handler);
			}
			else if (ex instanceof MissingServletRequestParameterException) {
				return handleMissingServletRequestParameter(
						(MissingServletRequestParameterException) ex, request, response, handler);
			}
			else if (ex instanceof ServletRequestBindingException) {
				return handleServletRequestBindingException(
						(ServletRequestBindingException) ex, request, response, handler);
			}
			else if (ex instanceof ConversionNotSupportedException) {
				return handleConversionNotSupported(
						(ConversionNotSupportedException) ex, request, response, handler);
			}
			else if (ex instanceof TypeMismatchException) {
				return handleTypeMismatch(
						(TypeMismatchException) ex, request, response, handler);
			}
			else if (ex instanceof HttpMessageNotReadableException) {
				return handleHttpMessageNotReadable(
						(HttpMessageNotReadableException) ex, request, response, handler);
			}
			else if (ex instanceof HttpMessageNotWritableException) {
				return handleHttpMessageNotWritable(
						(HttpMessageNotWritableException) ex, request, response, handler);
			}
			else if (ex instanceof MethodArgumentNotValidException) {
				return handleMethodArgumentNotValidException(
						(MethodArgumentNotValidException) ex, request, response, handler);
			}
			else if (ex instanceof MissingServletRequestPartException) {
				return handleMissingServletRequestPartException(
						(MissingServletRequestPartException) ex, request, response, handler);
			}
			else if (ex instanceof BindException) {
				return handleBindException((BindException) ex, request, response, handler);
			}
			else if (ex instanceof NoHandlerFoundException) {
				return handleNoHandlerFoundException(
						(NoHandlerFoundException) ex, request, response, handler);
			}
			else if (ex instanceof AsyncRequestTimeoutException) {
				return handleAsyncRequestTimeoutException(
						(AsyncRequestTimeoutException) ex, request, response, handler);
			}
		}
		catch (Exception handlerEx) {
			if (logger.isWarnEnabled()) {
				logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", handlerEx);
			}
		}
		return null;
	}

5.自定義實現(xiàn) HandlerExceptionResolver 處理異常,可以作為默認(rèn)的全局異常處理規(guī)則

@Order(value = Ordered.HIGHEST_PRECEDENCE)
@Component
public class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        try {
            response.sendError(521,"I love you !");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ModelAndView();
    }
}

SpringBoot異常處理的原理分析

ErrorViewResolver 實現(xiàn)自定義處理異常。

(1)底層調(diào)用response.sendError時 ,error請求就會默認(rèn)轉(zhuǎn)給basicErrorController,BasicErrorController專門來處理/error請求,適配4xx.html或者5xx.html頁面

(2)如果異常沒有任何解析器能處理,tomcat底層 也會調(diào)用response.sendError。error請求就會默認(rèn)轉(zhuǎn)給basicErrorController,BasicErrorController專門來處理/error請求,適配4xx.html或者5xx.html頁面。

(3)basicErrorController 要去的頁面地址是由 ErrorViewResolver這個錯誤視圖解析器決定的,即適配4xx.html或者5xx.html頁面。

關(guān)于“SpringBoot異常處理的原理分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

網(wǎng)站題目:SpringBoot異常處理的原理分析
URL鏈接:http://muchs.cn/article10/pipcgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)Google、域名注冊網(wǎng)站設(shè)計公司、網(wǎng)站排名自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司