最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringBoot異常處理流程及原理

 更新時(shí)間:2021年06月21日 16:10:51   作者:煎丶包  
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot異常處理流程及原理展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下

異常處理流程

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

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

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

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

在這里插入圖片描述

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

幾種異常處理方式及原理

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

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

在這里插入圖片描述

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專門來處理/error請(qǐng)求,適配4xx.html或者5xx.html頁面

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

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專門來處理/error請(qǐng)求,適配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.自定義實(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();
    }
}

在這里插入圖片描述

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

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

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

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

到此這篇關(guān)于詳解SpringBoot異常處理流程及原理的文章就介紹到這了,更多相關(guān)SpringBoot異常處理流程及原理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring cloud Feign使用@RequestLine遇到的坑

    spring cloud Feign使用@RequestLine遇到的坑

    這篇文章主要介紹了spring cloud Feign使用@RequestLine遇到的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 使用Java?Socket實(shí)現(xiàn)GPS定位數(shù)據(jù)處理

    使用Java?Socket實(shí)現(xiàn)GPS定位數(shù)據(jù)處理

    在許多應(yīng)用場(chǎng)景中,如車輛追蹤、移動(dòng)設(shè)備定位等,GPS定位數(shù)據(jù)的實(shí)時(shí)獲取和處理至關(guān)重要,本文將介紹如何使用Java?Socket編程來接收GPS設(shè)備發(fā)送的數(shù)據(jù)并進(jìn)行處理,需要的朋友可以參考下
    2024-07-07
  • java isPalindrome方法在密碼驗(yàn)證中的應(yīng)用

    java isPalindrome方法在密碼驗(yàn)證中的應(yīng)用

    這篇文章主要為大家介紹了java isPalindrome方法在密碼驗(yàn)證中的簡(jiǎn)單應(yīng)用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • SpringBoot集成Jasypt敏感信息加密的操作方法

    SpringBoot集成Jasypt敏感信息加密的操作方法

    這篇文章主要介紹了SpringBoot集成Jasypt加密敏感信息,包括敏感信息加密的作用,項(xiàng)目集成Jasypt方式詳解,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Java中ShardingSphere分庫(kù)分表實(shí)戰(zhàn)

    Java中ShardingSphere分庫(kù)分表實(shí)戰(zhàn)

    我們做項(xiàng)目的時(shí)候,數(shù)據(jù)量比較大,單表千萬級(jí)別的,需要分庫(kù)分表,本文主要介紹了Java中ShardingSphere分庫(kù)分表實(shí)戰(zhàn),感興趣的可以了解一下
    2021-09-09
  • 一文讀懂Spring中@Bean注解的核心作用

    一文讀懂Spring中@Bean注解的核心作用

    快速了解Spring框架中的@Bean注解?本文將帶你一鍵掌握其核心作用!只需一篇短文,揭示@Bean注解如何在Spring中定義bean實(shí)例,以及管理和裝配Bean的奧秘,閱讀指南,讓Spring開發(fā)更加得心應(yīng)手!
    2024-01-01
  • 淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件

    淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及Spr

    這篇文章主要介紹了淺談SpringMVC的攔截器(Interceptor)和Servlet 的過濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 學(xué)習(xí)Java HashMap,看這篇就夠了

    學(xué)習(xí)Java HashMap,看這篇就夠了

    這篇文章主要介紹了Java HashMap的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Springcloud-nacos實(shí)現(xiàn)配置和注冊(cè)中心的方法

    Springcloud-nacos實(shí)現(xiàn)配置和注冊(cè)中心的方法

    這篇文章主要介紹了Springcloud-nacos實(shí)現(xiàn)配置和注冊(cè)中心的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • springboot使用Validator校驗(yàn)方式

    springboot使用Validator校驗(yàn)方式

    這篇文章主要介紹了springboot使用Validator校驗(yàn)方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01

最新評(píng)論

靖西县| 东安县| 黄大仙区| 炉霍县| 哈尔滨市| 安丘市| 宿州市| 安岳县| 赞皇县| 兰州市| 广饶县| 安顺市| 青河县| 博兴县| 景泰县| 定结县| 阿拉善右旗| 清流县| 荥经县| 桂东县| 凭祥市| 韶关市| 田东县| 桦甸市| 河池市| 高台县| 淳化县| 休宁县| 凤冈县| 宁海县| 伊通| 石林| 西和县| 会理县| 萍乡市| 宣化县| 镇原县| 陇南市| 宝兴县| 阿图什市| 扶沟县|