springmvc異常處理解析#ExceptionHandlerExceptionResolver

来源:https://www.cnblogs.com/monianxd/archive/2022/07/27/16526098.html
-Advertisement-
Play Games

開頭 試想一下我們一般怎麼統一處理異常呢,答:切麵。但拋開切麵不講,如果對每一個controller方法拋出的異常做專門處理,那麼著實太費勁了,有沒有更好的方法呢?當然有,就是本篇文章接下來要介紹的springmvc的異常處理機制,用到了ControllerAdvice和ExceptionHandl ...


開頭

試想一下我們一般怎麼統一處理異常呢,答:切麵。但拋開切麵不講,如果對每一個controller方法拋出的異常做專門處理,那麼著實太費勁了,有沒有更好的方法呢?當然有,就是本篇文章接下來要介紹的springmvc的異常處理機制,用到了ControllerAdvice和ExceptionHandler註解,有點切麵的感覺哈哈。

 

1.ExceptionHandlerExceptionResolver

首先從springmvc的異常處理解析器開始講,當執行完controller方法後,不管有沒有異常產生都會調用DispatcherServlet#doDispatch()方法中的processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); 方法,接著會判斷是否有異常,若無異常則走正常流程,若有異常則需要進行處理 mv = processHandlerException(request, response, handler, exception);  再接著就是遍歷spring已經註冊的異常處理解析器直到有處理器返回mav

private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
			@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
			@Nullable Exception exception) throws Exception {

		if (exception != null) {
			if (exception instanceof ModelAndViewDefiningException) {
				logger.debug("ModelAndViewDefiningException encountered", exception);
				mv = ((ModelAndViewDefiningException) exception).getModelAndView();
			}
			else {
				Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
				// 執行處理器產生的異常處理
				mv = processHandlerException(request, response, handler, exception);
				// 是否有異常視圖返回
				errorView = (mv != null);
			}
		}

		// Did the handler return a view to render? 處理程式是否返回要渲染的視圖
		if (mv != null && !mv.wasCleared()) {
			// 渲染視圖
			render(mv, request, response);
			if (errorView) {
				WebUtils.clearErrorRequestAttributes(request);
			}
		}
		else {
			if (logger.isDebugEnabled()) {
				logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName() +
						"': assuming HandlerAdapter completed request handling");
			}
		}
	}
	@Nullable
	protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
			@Nullable Object handler, Exception ex) throws Exception {

		// Check registered HandlerExceptionResolvers...
		ModelAndView exMv = null;
		if (this.handlerExceptionResolvers != null) {
			for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
				exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
				if (exMv != null) {
					break;
				}
			}
		}
		if (exMv != null) {
			// 無視圖view
			if (exMv.isEmpty()) {
				request.setAttribute(EXCEPTION_ATTRIBUTE, ex);
				return null;
			}
			// We might still need view name translation for a plain error model...
			if (!exMv.hasView()) {
				String defaultViewName = getDefaultViewName(request);
				if (defaultViewName != null) {
					exMv.setViewName(defaultViewName);
				}
			}
			WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
			return exMv;
		}

		throw ex;
	}

 

其中最重要也是最常使用的一個處理器就是ExceptionHandlerExceptionResolver,下麵將著重介紹它,先來看看這個類的繼承結構圖,實現了InitializingBean介面,在這個bean創建完成之前會調用生命周期初始化方法afterPropertiesSet(),這裡麵包含了對@ControllerAdvice註解的解析,初始化完後的信息供後續解析異常使用。

實現HandlerExceptionResolver介面,實現解析方法resolveException()

public interface HandlerExceptionResolver {

	/**
	 * Try to resolve the given exception that got thrown during handler execution,
	 * returning a {@link ModelAndView} that represents a specific error page if appropriate.
	 * <p>The returned {@code ModelAndView} may be {@linkplain ModelAndView#isEmpty() empty}
	 * to indicate that the exception has been resolved successfully but that no view
	 * should be rendered, for instance by setting a status code.
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @param handler the executed handler, or {@code null} if none chosen at the
	 * time of the exception (for example, if multipart resolution failed)
	 * @param ex the exception that got thrown during handler execution
	 * @return a corresponding {@code ModelAndView} to forward to,
	 * or {@code null} for default processing in the resolution chain
	 */
	@Nullable
	ModelAndView resolveException(
			HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex);

}
@Override
public void afterPropertiesSet() {
    // Do this first, it may add ResponseBodyAdvice beans
    // 初始化異常註解 @ControllerAdvice
    initExceptionHandlerAdviceCache();
}

private void initExceptionHandlerAdviceCache() {
    if (getApplicationContext() == null) {
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Looking for exception mappings: " + getApplicationContext());
    }

    // 解析有@ControllerAdvice註解的bean,並將這個bean構建成ControllerAdviceBean對象
    List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
    // 將ControllerAdviceBean根據order排序
    AnnotationAwareOrderComparator.sort(adviceBeans);

    for (ControllerAdviceBean adviceBean : adviceBeans) {
        Class<?> beanType = adviceBean.getBeanType();
        if (beanType == null) {
            throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
        }
        ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
        // mappedMethods 映射不為空
        if (resolver.hasExceptionMappings()) {
            // 添加到緩存中
            this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
            if (logger.isInfoEnabled()) {
                logger.info("Detected @ExceptionHandler methods in " + adviceBean);
            }
        }
        // 若實現了ResponseBodyAdvice介面(暫不介紹)
        if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
            this.responseBodyAdvice.add(adviceBean);
            if (logger.isInfoEnabled()) {
                logger.info("Detected ResponseBodyAdvice implementation in " + adviceBean);
            }
        }
    }
}

 ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType); 這行代碼會解析擁有@ControllerAdvice 註解的class,並且會遍歷class中帶有 @ExceptionHandler 註解的方法,獲取方法註解帶有的異常類型,將異常類型和方法放入到mappedMethods中供後面獲取,獲取的時候若對應處理此異常類型的method有多個,則需要進行排序,選取一個異常類型與method ExceptionHandler註解異常類型最近的一個(深度最小的那個也即是繼承關係最少的那個)具體代碼如下:

ExceptionHandlerMethodResolver
public class ExceptionHandlerMethodResolver {

	/**
	 * A filter for selecting {@code @ExceptionHandler} methods.
	 */
	public static final MethodFilter EXCEPTION_HANDLER_METHODS = method ->
			(AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null);


	/**
	 * 異常類型與方法的映射map
	 */
	private final Map<Class<? extends Throwable>, Method> mappedMethods = new HashMap<>(16);

	/**
	 * 緩存,用來存儲先前碰到過的異常類型與處理方法的映射
	 */
	private final Map<Class<? extends Throwable>, Method> exceptionLookupCache = new ConcurrentReferenceHashMap<>(16);


	/**
	 * A constructor that finds {@link ExceptionHandler} methods in the given type.
	 * @param handlerType the type to introspect
	 */
	public ExceptionHandlerMethodResolver(Class<?> handlerType) {
		// 獲取並遍歷@ExceptionHandler註解的方法
		for (Method method : MethodIntrospector.selectMethods(handlerType, EXCEPTION_HANDLER_METHODS)) {
			for (Class<? extends Throwable> exceptionType : detectExceptionMappings(method)) {
				addExceptionMapping(exceptionType, method);
			}
		}
	}


	/**
	 * Extract exception mappings from the {@code @ExceptionHandler} annotation first,
	 * and then as a fallback from the method signature itself.
	 */
	@SuppressWarnings("unchecked")
	private List<Class<? extends Throwable>> detectExceptionMappings(Method method) {
		List<Class<? extends Throwable>> result = new ArrayList<>();
		// 將註解ExceptionHandler value值異常添加到result中
		detectAnnotationExceptionMappings(method, result);
		// 註解值為空的話再去獲取參數的異常類型
		if (result.isEmpty()) {
			for (Class<?> paramType : method.getParameterTypes()) {
				if (Throwable.class.isAssignableFrom(paramType)) {
					result.add((Class<? extends Throwable>) paramType);
				}
			}
		}
		if (result.isEmpty()) {
			throw new IllegalStateException("No exception types mapped to " + method);
		}
		return result;
	}

	protected void detectAnnotationExceptionMappings(Method method, List<Class<? extends Throwable>> result) {
		ExceptionHandler ann = AnnotationUtils.findAnnotation(method, ExceptionHandler.class);
		Assert.state(ann != null, "No ExceptionHandler annotation");
		result.addAll(Arrays.asList(ann.value()));
	}

	private void addExceptionMapping(Class<? extends Throwable> exceptionType, Method method) {
		// 將異常類型以及對應的method添加到map中,且異常類型不能有重覆否則會報錯
		Method oldMethod = this.mappedMethods.put(exceptionType, method);
		if (oldMethod != null && !oldMethod.equals(method)) {
			throw new IllegalStateException("Ambiguous @ExceptionHandler method mapped for [" +
					exceptionType + "]: {" + oldMethod + ", " + method + "}");
		}
	}

	/**
	 * Whether the contained type has any exception mappings.
	 */
	public boolean hasExceptionMappings() {
		return !this.mappedMethods.isEmpty();
	}

	/**
	 * Find a {@link Method} to handle the given exception.
	 * Use {@link ExceptionDepthComparator} if more than one match is found.
	 * @param exception the exception
	 * @return a Method to handle the exception, or {@code null} if none found
	 */
	@Nullable
	public Method resolveMethod(Exception exception) {
		return resolveMethodByThrowable(exception);
	}

	/**
	 * Find a {@link Method} to handle the given Throwable.
	 * Use {@link ExceptionDepthComparator} if more than one match is found.
	 * @param exception the exception
	 * @return a Method to handle the exception, or {@code null} if none found
	 * @since 5.0
	 */
	@Nullable
	public Method resolveMethodByThrowable(Throwable exception) {
		Method method = resolveMethodByExceptionType(exception.getClass());
		if (method == null) {
			Throwable cause = exception.getCause();
			if (cause != null) {
				method = resolveMethodByExceptionType(cause.getClass());
			}
		}
		return method;
	}

	/**
	 * Find a {@link Method} to handle the given exception type. This can be
	 * useful if an {@link Exception} instance is not available (e.g. for tools).
	 * @param exceptionType the exception type
	 * @return a Method to handle the exception, or {@code null} if none found
	 */
	@Nullable
	public Method resolveMethodByExceptionType(Class<? extends Throwable> exceptionType) {
		Method method = this.exceptionLookupCache.get(exceptionType);
		if (method == null) {
			method = getMappedMethod(exceptionType);
			this.exceptionLookupCache.put(exceptionType, method);
		}
		return method;
	}

	/**
	 * Return the {@link Method} mapped to the given exception type, or {@code null} if none.
	 */
	@Nullable
	private Method getMappedMethod(Class<? extends Throwable> exceptionType) {
		List<Class<? extends Throwable>> matches = new ArrayList<>();
		for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) {
			if (mappedException.isAssignableFrom(exceptionType)) {
				matches.add(mappedException);
			}
		}
		if (!matches.isEmpty()) {
			// exceptionType 到matchs父類異常類型的深度
			matches.sort(new ExceptionDepthComparator(exceptionType));
			return this.mappedMethods.get(matches.get(0));
		}
		else {
			return null;
		}
	}

}
@Override
@Nullable
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
                                                       HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) {

    // exception為controller方法拋出的異常
    // 根據異常及其類型從上述的mappedMethods中獲取對應的方法,再獲取方法所在的對象 封裝成ServletInvocableHandlerMethod
    ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
    if (exceptionHandlerMethod == null) {
        return null;
    }

    // 設置參數解析器,主要用來獲取方法的參數值的,供後續反射調用方法
    if (this.argumentResolvers != null) {
        exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
    }
    // 設置返回值解析器,當執行完方法後獲取返回值,對返回值進行處理 或返回視圖或將結果寫入到response
    if (this.returnValueHandlers != null) {
        exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
    }

    ServletWebRequest webRequest = new ServletWebRequest(request, response);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();

    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Invoking @ExceptionHandler method: " + exceptionHandlerMethod);
        }
        Throwable cause = exception.getCause();
        if (cause != null) {
            // Expose cause as provided argument as well
            // 執行異常處理方法,也就是我們的自定義的異常處理方法
            exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod);
        }
        else {
            // Otherwise, just the given exception as-is
            exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
        }
    }
    catch (Throwable invocationEx) {
        // Any other than the original exception is unintended here,
        // probably an accident (e.g. failed assertion or the like).
        if (invocationEx != exception && logger.isWarnEnabled()) {
            logger.warn("Failed to invoke @ExceptionHandler method: " + exceptionHandlerMethod, invocationEx);
        }
        // Continue with default processing of the original exception...
        return null;
    }

    // 根據後續的返回值解析器設置的,將返回值寫入到response中了直接返回空的mav
    if (mavContainer.isRequestHandled()) {
        return new ModelAndView();
    }
    else {
        ModelMap model = mavContainer.getModel();
        HttpStatus status = mavContainer.getStatus();
        ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, status);
        mav.setViewName(mavContainer.getViewName());
        // (this.view instanceof String)
        if (!mavContainer.isViewReference()) {
            mav.setView((View) mavContainer.getView());
        }
        if (model instanceof RedirectAttributes) {
            Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
            RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
        }
        return mav;
    }
}

exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod); 此方法執行完成後已經完成了異常處理方法的調用,若方法返回值為視圖ModelAndView或其他視圖類型,則還需要藉助視圖解析器如InternalResourceViewResolver對視圖進行解析渲染,若為其他類型的值則將值寫入到response響應中。

 

2. demo

Controller類方法:

@Controller
@RequestMapping(value = "test")
public class HelloWorldController{

  @Data
  public static class User {
    private String username;

    private Integer age;

    private String address;
  }


  @RequestMapping(value = "user/get", method = RequestMethod.POST)
  @ResponseBody
  public Object testObject(@RequestBody @Valid User user, @RequestParam String address) {
    user.setAddress(address);
    // 這裡特意拋出RuntimeException異常
    throw new RuntimeException("this is a exception");
  }

}

ExceptionHandlerController異常處理類

@ControllerAdvice
@ResponseBody
public class ExceptionHandlerController {

  @ExceptionHandler(value = Exception.class)
  public Object handleException(Exception e) {
    return CommonResult.fail("Exception:" + e.getMessage());
  }

  @ExceptionHandler(value = RuntimeException.class)
  public Object handlerRuntimeException(Exception e) {
    return CommonResult.fail("handlerRuntimeException:" + e.getMessage());
  }
}

ExceptionHandlerController類中定義了兩個異常處理方法,一個處理Exception異常,一個處理RuntimeException異常,那個根據controller方法拋出的異常RuntimeException再結合上面的分析(RuntimeException到RuntimeException深度為0,RuntimeException到Exception中間繼承了一次深度為1)可以得出拋出異常類型的處理方法為handlerRuntimeException 方法。 運行程式結果如下:

 

結語

初步解析ExceptionHandlerExceptionResolver源碼,若寫的有誤或者有不理解的地方,歡迎指出討論~


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 首先,瞭解預解析之前先看兩個問題 1.大家思考下 這個結果會是多少呢? console.log(num); var num=10; 結果是 undefined 2.這個輸出結果又會是多少呢? fun(); var fun=function(){ console.log(22); } 顯然這個結果報錯 ...
  • 條件控制語句及表達式 運算符及表達式 1.()前面不能直接用++ console.log(++(a--)); //() 不能和++ 一起使用 2.str 與 Number值比較(字元串比較時會自動變為Number值) console.log('123A'>213);//false 自動轉為numbe ...
  • 在電腦發展的早期,一直都是集中式計算,計算能力依賴大型電腦。隨著互聯網的發展,繁重的業務需要巨大的計算能力才能完成,而集中式計算無法滿足要求,大型電腦的價格也非常昂貴。分散式計算將任務分解成更小的部分,分配給多台電腦處理,這樣可以節約整體計算時間,大大提高計算效率。互聯網大型網站往往面臨高並... ...
  • hello,大家好呀,我是小樓。今天不寫BUG,來聊一聊註冊中心。 標題本來想叫《如何設計一個註冊中心》,但網上已經有好多類似標題的文章了。所以打算另闢蹊徑,換個角度,如何組裝一個註冊中心。 組裝意味著不必從0開始造輪子,這也比較符合許多公司對待自研基礎組件的態度。 知道如何組裝一個註冊中心有什麼用 ...
  • 一、freemarker介紹 FreeMarker 是一款 模板引擎 即一種基於模板和要改變的數據, 並用來生成輸出文本(HTML網頁,電子郵件,配置文件,源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程式員可以嵌入他們所開發產品的組件 模板編寫為FreeMarker T ...
  • C++語言全盤繼承了C語言的標準庫,其中包換非常豐富的系統函數,例如輸入/輸出函數、數學函數、字元串處理函數和動態記憶體分配函數等。C++語言另外又增加了一些新的庫,我們把C++語言新增的這部分庫稱為C++標準庫。C++語言的模板技術包括函數模板和類模板。模板技術是一種代碼重用技術,函數和類是C++語 ...
  • 目錄 一.簡介 二.效果演示 三.源碼下載 四.猜你喜歡 零基礎 OpenGL (ES) 學習路線推薦 : OpenGL (ES) 學習目錄 >> OpenGL ES 基礎 零基礎 OpenGL (ES) 學習路線推薦 : OpenGL (ES) 學習目錄 >> OpenGL ES 轉場 零基礎 O ...
  • 本文包含JSP的原理剖析、基礎語法和指令、內置對象和作用域、JSP及JSTL標簽及JavaBean等JSP的知識都在這裡了 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...