這篇文章主要介紹“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
拋出異常
進入視圖解析流程,并渲染頁面,發(fā)生異常時,參數(shù)mv
為空,傳入捕獲的異常dispatchException
處理handler
發(fā)生的異常,處理完成返回ModelAndView
(1)遍歷所有的HandlerExceptionResolvers
,找到可以處理當(dāng)前異常的解析器來解析異常
(2)調(diào)用resolveException
解析異常,傳入request
和response
對象,哪個方法,發(fā)生的異常,然后自定義異常處理返回ModelAndView
(3)系統(tǒng)默認(rèn)的異常解析器
① DefaultErrorAttributes
先來處理異常,把異常信息保存到request
域并返回null
② ExceptionHandlerExceptionResolver
用來處理標(biāo)注了@ExceptionHandler
注解的方法異常
③ ResponseStatusExceptionResolver
用來處理標(biāo)注了@ResponseStatus
注解的方法異常
④ DefaultHandlerExceptionResolver
默認(rèn)的處理器異常解析器,處理一些常見的異常
(4)如果沒有任何解析器能夠處理異常,異常就會拋出
(5)如果沒有任何解析器能夠處理當(dāng)前異常,最終就會發(fā)送/error
請求,將保存的異常信息轉(zhuǎn)發(fā)到/error
。BasicErrorController
專門來處理/error
請求,BasicErrorController
會遍歷所有的ErrorViewResolver
解析錯誤視圖,如果沒有自定義的錯誤視圖解析器,就會使用默認(rèn)的DefaultErrorViewResolver
,會把響應(yīng)碼作為錯誤頁的地址,模板引擎最終響應(yīng)這個頁面。
1.自定義錯誤頁,error/404.html
、error/5xx.html
。有精確的錯誤狀態(tài)碼頁面就匹配精確,沒有就找 4xx.html
,如果都沒有就觸發(fā)白頁
2.使用@ControllerAdvice
和@ExceptionHandler
處理全局異常,底層是ExceptionHandlerExceptionResolver
支持的
3.使用@ResponseStatus
和自定義異常。底層是 ResponseStatusExceptionResolver
,底層調(diào)用 response.sendError(statusCode, resolvedReason)
,Tomcat會收到一個error
。請求最后new
一個空的ModelAndView
返回,這樣任何處理解析器都處理不了當(dāng)前的異常,最終就會發(fā)送/error
請求,BasicErrorController
專門來處理/error
請求,適配4xx.html
或者5xx.html
頁面
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(); } }
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)