欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpringBoot異常處理的原理分析

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

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

異常處理流程

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

SpringBoot異常處理的原理分析

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

SpringBoot異常處理的原理分析

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

SpringBoot異常處理的原理分析

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

SpringBoot異常處理的原理分析

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

SpringBoot異常處理的原理分析

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

SpringBoot異常處理的原理分析

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

SpringBoot異常處理的原理分析

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

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

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

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

SpringBoot異常處理的原理分析

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

幾種異常處理方式及原理

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

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

SpringBoot異常處理的原理分析

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

SpringBoot異常處理的原理分析

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

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.自定義實(shí)現(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 實(shí)現(xiàn)自定義處理異常。

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

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

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

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

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

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

廣告

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

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