springMVC之HandlerExceptionResolver使用
請求異常的處理
Handler查找以及執(zhí)行期間可能會出現異常,需要對其進行處理,HandlerExceptionResolver就被設計出來了,
大致邏輯如下:
// 此段邏輯可以在dispatcherServlet中找到相似部分
ModelAndView mv = null;
try{
?? ?mv = hanlder.handle();
}catch(Exception e){
?? ?mv = handlerExceptionResolver.handle();
}springMVC也是這么設計的,當然比這要復雜一點,我們先來看一下HandlerExceptionResolver這個接口設計。
public interface HandlerExceptionResolver {
? ? // 處理異常,返回視圖信息
?? ?@Nullable
?? ?ModelAndView resolveException(
?? ??? ??? ?HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex);
}可用的HandlerExceptionResolver

AbstractHandlerExceptionResolver這個抽象類的設計可以幫助我們針對不同的handler配置不同的HandlerExceptionResolver。
// AbstractHandlerExceptionResolver中解析異常的實現
public ModelAndView resolveException(
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
// 檢查這個解析器是否適用于這個處理器
if (shouldApplyTo(request, handler)) {
// 添加響應頭,阻止響應緩存
prepareResponse(ex, response);
// 解析異常 交給子類覆蓋實現
ModelAndView result = doResolveException(request, response, handler, ex);
// 日志記錄,這里我將代碼省略了
return result;
}else {
return null;
}
}
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {
if (handler != null) {
// 可以通過設置mappedHandlers或mappedHandlerClasses來指定只為某個handler解析
if (this.mappedHandlers != null && this.mappedHandlers.contains(handler)) {
return true;
}
if (this.mappedHandlerClasses != null) {
for (Class<?> handlerClass : this.mappedHandlerClasses) {
if (handlerClass.isInstance(handler)) {
return true;
}
}
}
}
// 這里就是判斷mappedHandlers或mappedHandlerClasses是否為空,都為空返回false
// 意味著異常解析器可以適用于任何handler
return !hasHandlerMappings();
}
接下來我們來看看不同子類對于doResolveException方法的實現。
1. SimpleMappingExceptionResolver
首先我們來看一下該類提供了哪些屬性供我們進行設置。
?? ?// 1 配置的異常映射 key為異常名稱 value為視圖名稱 ?? ?private Properties exceptionMappings; ?? ?// 2 排除的異常類型數組 ?? ?private Class<?>[] excludedExceptions; ?? ?// 3 默認的錯誤視圖名稱 ?? ?private String defaultErrorView; ?? ?// 4 默認的響應狀態(tài)碼 ?? ?private Integer defaultStatusCode; ?? ?// 5 key為異常名,value為響應狀態(tài)碼 ?? ?private Map<String, Integer> statusCodes = new HashMap<>();
protected ModelAndView doResolveException(
?? ??? ??? ?HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
?? ??? ?// 確定視圖名稱。
?? ??? ?String viewName = determineViewName(ex, request);
?? ??? ?if (viewName != null) {
?? ??? ??? ?// 根據視圖名稱去statusCodes中確定響應狀態(tài)碼
?? ??? ??? ?Integer statusCode = determineStatusCode(request, viewName);
?? ??? ??? ?if (statusCode != null) {
?? ??? ??? ??? ?applyStatusCodeIfPossible(request, response, statusCode);
?? ??? ??? ?}
?? ??? ??? ?return getModelAndView(viewName, ex, request);
?? ??? ?}else {
?? ??? ??? ?return null;
?? ??? ?}
?? ?}protected String determineViewName(Exception ex, HttpServletRequest request) {
?? ??? ?String viewName = null;
?? ??? ?// 如果排除的異常數組中包含發(fā)生的異常,則返回null
?? ??? ?if (this.excludedExceptions != null) {
?? ??? ??? ?for (Class<?> excludedEx : this.excludedExceptions) {
?? ??? ??? ??? ?if (excludedEx.equals(ex.getClass())) {
?? ??? ??? ??? ??? ?return null;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?// 檢查特定的異常映射。
?? ??? ?if (this.exceptionMappings != null) {
?? ??? ??? ?viewName = findMatchingViewName(this.exceptionMappings, ex);
?? ??? ?}
?? ??? ?// 定義了默認錯誤視圖
?? ??? ?if (viewName == null && this.defaultErrorView != null) {
?? ??? ??? ?viewName = this.defaultErrorView;
?? ??? ?}
?? ??? ?return viewName;
?? ?}protected String findMatchingViewName(Properties exceptionMappings, Exception ex) {
?? ??? ?String viewName = null;
?? ??? ?String dominantMapping = null;
?? ??? ?int deepest = Integer.MAX_VALUE;
?? ??? ?for (Enumeration<?> names = exceptionMappings.propertyNames(); names.hasMoreElements();) {
?? ??? ??? ?String exceptionMapping = (String) names.nextElement();
?? ??? ??? ?// depth =0 表示剛好找到;depth =-1 表示沒找到,這是一個遞歸方法,
?? ??? ??? ?// 會一直沿著異常的繼承結構向上找,每向上一層,depth+1
?? ??? ??? ?int depth = getDepth(exceptionMapping, ex);
?? ??? ??? ?if (depth >= 0 && (depth < deepest || (depth == deepest &&
?? ??? ??? ??? ??? ?dominantMapping != null && exceptionMapping.length() > dominantMapping.length()))) {
?? ??? ??? ??? ?// 這里將深度賦值了,意味著一旦有匹配的異常結果時,即使下一次更匹配,但是
?? ??? ??? ??? ?// depth < deepest這個條件也無法滿足
?? ??? ??? ??? ?deepest = depth;
?? ??? ??? ??? ?dominantMapping = exceptionMapping;
?? ??? ??? ??? ?viewName = exceptionMappings.getProperty(exceptionMapping);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return viewName;
?? ?}?? ?private int getDepth(String exceptionMapping, Class<?> exceptionClass, int depth) {
?? ??? ?// 需要注意,這里使用的是contains,如果異常名稱不適用全限定名
?? ??? ?// 一旦出現多個異常映射項匹配的情況,將直接選擇第一個匹配的結果
?? ??? ?if (exceptionClass.getName().contains(exceptionMapping)) {
?? ??? ??? ?return depth;
?? ??? ?}
?? ??? ?if (exceptionClass == Throwable.class) {
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?return getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);
?? ?}demo
/**
?* 通過SimpleMappingExceptionResolver做全局異常處理
?*/
@Configuration
public class ExceptionConfig {
?? ?@Bean
?? ?public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
?? ??? ?SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();?? ??? ?
?? ??? ?Properties exceptionMappings= new Properties();?? ??? ?
?? ??? ?/**
?? ??? ? * 這里是不建議的使用方式,如果出現異常java.lang.ArithmeticException,最終得到的視圖卻為
?? ??? ? * error,這是我們不希望的,所以請使用全限定名
?? ??? ? */
?? ??? ?mappers.put("Exception", "error");
?? ??? ?mappers.put("ArithmeticException", "error1");
?? ??? ?mappers.put("java.lang.ArithmeticException", "error2");
?? ??? ?resolver.setExceptionMappings(exceptionMappings);
?? ??? ?resolver.setDefaultErrorView("error3");
?? ??? ?return resolver;
?? ?}
}2. DefaultHandlerExceptionResolver
(解決標準的Spring MVC異常并將其轉換為相應的HTTP狀態(tài)碼),使用時我們不用設置order,默認的最小的優(yōu)先級。
protected ModelAndView doResolveException(
?? ??? ??? ?HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
?? ??? ?try {
?? ??? ??? ?if (ex instanceof HttpRequestMethodNotSupportedException) {
?? ??? ??? ??? ?return handleHttpRequestMethodNotSupported(
?? ??? ??? ??? ??? ??? ?(HttpRequestMethodNotSupportedException) ex, request, response, handler);
?? ??? ??? ?}
?? ??? ??? ?// 這里有很多if語句,就是針對標準的Spring MVC異常,返回對應的狀態(tài)碼進行處理
?? ??? ?}catch (Exception handlerEx) {
?? ??? ??? ?// 日志打印...
?? ??? ?}
?? ??? ?return null;
?? ?}3. ResponseStatusExceptionResolver
protected ModelAndView doResolveException(
?? ??? ??? ?HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
?? ??? ?try {
?? ??? ??? ?if (ex instanceof ResponseStatusException) {
?? ??? ??? ??? ?return resolveResponseStatusException((ResponseStatusException) ex, request, response, handler);
?? ??? ??? ?}
?? ??? ??? ?// 找到異常上使用的ResponseStatus注解
?? ??? ??? ?ResponseStatus status = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class);
?? ??? ??? ?if (status != null) {
?? ??? ??? ??? ?return resolveResponseStatus(status, request, response, handler, ex);
?? ??? ??? ?}
?? ??? ??? ?// ResponseStatus為空,異常鏈仍未結束,遞歸調用
?? ??? ??? ?if (ex.getCause() instanceof Exception) {
?? ??? ??? ??? ?return doResolveException(request, response, handler, (Exception) ex.getCause());
?? ??? ??? ?}
?? ??? ?}catch (Exception resolveEx) {
?? ??? ??? ?...
?? ??? ?}
?? ??? ?return null;
?? ?}ResponseStatus注解使用方式挺多的,這里是其中一種,就是在自定義異常的類上添加此注解。還有其他用法大家可以去看看這篇文章,@ResponseStatus注解的更多用法。
4. ExceptionHandlerExceptionResolver
在看這個類解析異常的方法之前,我們先認識一下兩個緩存,分別為局部和全局異常方法解析器映射緩存,源碼中也就是這兩個變量。
?? ?// 局部異常方法解析器緩存,下面統(tǒng)稱為局部緩存 ?? ?private final Map<Class<?>, ExceptionHandlerMethodResolver> exceptionHandlerCache = ?? ??? ??? ?new ConcurrentHashMap<>(64); ?? ?// 全局異常方法解析器緩存,下面統(tǒng)稱為全局緩存 ?? ?private final Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> exceptionHandlerAdviceCache = ?? ??? ??? ?new LinkedHashMap<>();
全局緩存在bean初始化的時候就會加載,將會把容器中含有注解ControllerAdvice的bean收集起來。
?? ?// bean 初始化的時候會加載這個方法
?? ?public void afterPropertiesSet() {
?? ??? ?// 初始化全局緩存
?? ??? ?initExceptionHandlerAdviceCache();
?? ??? ?if (this.argumentResolvers == null) {
?? ??? ??? ?// 設置請求參數解析器
?? ??? ?}
?? ??? ?if (this.returnValueHandlers == null) {
?? ??? ??? ?// 設置返回值解析器
?? ??? ?}
?? ?}
?? ?private void initExceptionHandlerAdviceCache() {
?? ??? ?if (getApplicationContext() == null) {
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?// 獲取含有注解ControllerAdvice的bean
?? ??? ?List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
?? ??? ?for (ControllerAdviceBean adviceBean : adviceBeans) {
?? ??? ??? ?Class<?> beanType = adviceBean.getBeanType();
?? ??? ??? ?if (beanType == null) {
?? ??? ??? ??? ?throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean);
?? ??? ??? ?}
?? ??? ??? ?ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
?? ??? ??? ?if (resolver.hasExceptionMappings()) {
?? ??? ??? ??? ?// 加入全局緩存
?? ??? ??? ??? ?this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
?? ??? ??? ?}
?? ??? ??? ?// MARK:如果該類實現了ResponseBodyAdvice接口
?? ??? ??? ?if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) {
?? ??? ??? ??? ?this.responseBodyAdvice.add(adviceBean);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?// ...
?? ?}有關MARK部分ResponseBodyAdvice接口的用處,這里不展開了。
而局部緩存是在解析異常的方法中動態(tài)加載的。
?? ?protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
?? ??? ??? ?HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) {
?? ??? ?// 根據異常類型,處理器找到處理異常的方法
?? ??? ?ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
?? ??? ?// 異常方法調用...
?? ??? ?// 返回ModelAndView...
?? ?}protected ServletInvocableHandlerMethod getExceptionHandlerMethod(
?? ??? ??? ?@Nullable HandlerMethod handlerMethod, Exception exception) {
?? ?if (handlerMethod != null) {
?? ??? ??? ?handlerType = handlerMethod.getBeanType();
?? ??? ??? ?// 從局部緩存中查找,找不到,則構建一個
?? ??? ??? ?ExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(handlerType);
?? ??? ??? ?if (resolver == null) {
?? ??? ??? ??? ?resolver = new ExceptionHandlerMethodResolver(handlerType);
?? ??? ??? ??? ?this.exceptionHandlerCache.put(handlerType, resolver);
?? ??? ??? ?}
?? ??? ??? ?// 解析異常獲取對應方法
?? ??? ??? ?Method method = resolver.resolveMethod(exception);
?? ??? ??? ?if (method != null) {
?? ??? ??? ??? ?return new ServletInvocableHandlerMethod(handlerMethod.getBean(), method);
?? ??? ??? ?}
?? ??? ??? ?// 如果是代理類,則要獲取到原目標類型
?? ??? ??? ?if (Proxy.isProxyClass(handlerType)) {
?? ??? ??? ??? ?handlerType = AopUtils.getTargetClass(handlerMethod.getBean());
?? ??? ??? ?}
?? ??? ?}
?? ??? ?// 局部緩存沒有找到,則去全局緩存中找
?? ??? ?for (Map.Entry<ControllerAdviceBean, ExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache.entrySet()) {
?? ??? ??? ?ControllerAdviceBean advice = entry.getKey();
?? ??? ??? ?// MARK:檢查是否應該通過給定的bean類型?
?? ??? ??? ?if (advice.isApplicableToBeanType(handlerType)) {
?? ??? ??? ??? ?ExceptionHandlerMethodResolver resolver = entry.getValue();
?? ??? ??? ??? ?Method method = resolver.resolveMethod(exception);
?? ??? ??? ??? ?if (method != null) {
?? ??? ??? ??? ??? ?return new ServletInvocableHandlerMethod(advice.resolveBean(), method);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?return null;
}?? ??? ?上述代碼MARK部分涉及到另一知識點,有關注解ControllerAdvice的設置部分,這里就不展開了。
同前面一樣,在看解析異常獲取對應方法前,我先介紹另一個緩存-異常映射方法緩存。這個緩存在ExceptionHandlerMethodResolver實例化的時候被加載。
?? ?// 用于選擇@ExceptionHandler方法的過濾器。
?? ?public static final MethodFilter EXCEPTION_HANDLER_METHODS = method ->
?? ??? ??? ?AnnotatedElementUtils.hasAnnotation(method, ExceptionHandler.class);
?? ?// 異常映射方法緩存 key為異常類型 value為方法 ?? ??? ?
?? ?private final Map<Class<? extends Throwable>, Method> mappedMethods = new HashMap<>(16);
?? ?
?? ?public ExceptionHandlerMethodResolver(Class<?> handlerType) {
?? ??? ?for (Method method : MethodIntrospector.selectMethods(handlerType, EXCEPTION_HANDLER_METHODS)) {
?? ??? ??? ?// 檢測方法映射的異常
?? ??? ??? ?for (Class<? extends Throwable> exceptionType : detectExceptionMappings(method)) {
?? ??? ??? ??? ?// 加入緩存
?? ??? ??? ??? ?addExceptionMapping(exceptionType, method);
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?
?? ?private List<Class<? extends Throwable>> detectExceptionMappings(Method method) {
?? ??? ?List<Class<? extends Throwable>> result = new ArrayList<>();
?? ??? ?// 獲取注解ExceptionHandler的value屬性
?? ??? ?detectAnnotationExceptionMappings(method, result);
?? ??? ?// 如果注解value沒有設置
?? ??? ?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;
?? ?}了解這個緩存后,再回過頭來看解析異常獲取對應方法,其實就是從緩存中找而已。
?? ?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()) {
?? ??? ??? ?// 有可能出現同一個異常匹配到多個映射的情況,這里按異常層級關系,從小大大排序
?? ??? ??? ?matches.sort(new ExceptionDepthComparator(exceptionType));
?? ??? ??? ?// 取最小層級的
?? ??? ??? ?return this.mappedMethods.get(matches.get(0));
?? ??? ?}else {
?? ??? ??? ?return null;
?? ??? ?}
?? ?}以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java swagger ui 添加header請求頭參數的方法
今天小編就為大家分享一篇java swagger ui 添加header請求頭參數的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Java創(chuàng)建可執(zhí)行的Jar文件的方法實踐
創(chuàng)建的可執(zhí)行Jar文件實際就是在原始Jar的清單文件中添加了Main-Class的配置,本文主要介紹了Java創(chuàng)建可執(zhí)行的Jar文件的方法實踐,感興趣的可以了解一下2023-12-12
關于Redis鍵值出現\xac\xed\x00\x05t\x00&錯誤的解決方法
這篇文章主要介紹了關于Redis鍵值出現\xac\xed\x00\x05t\x00&的解決方法,出現該問題的原因是, redis template向redis存放使用java對象序列化的值,序列化方式和string的一般方式不同,需要的朋友可以參考下2023-08-08
談談Java利用原始HttpURLConnection發(fā)送POST數據
這篇文章主要給大家介紹java利用原始httpUrlConnection發(fā)送post數據,設計到httpUrlConnection類的相關知識,感興趣的朋友跟著小編一起學習吧2015-10-10

