springmvc揭秘异常处理机制

HandlerExceptionResolver异常处理

HandlerExceptionResolver是专门进行异常处理的,在render之前进行工作,从异常中解析出ModelAndView

复制代码
public interface HandlerExceptionResolver {
  
   ModelAndView resolveException(
         HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);

}
  • DefaultHandlerExceptionResolver 根据不同类型的异常来进行解析,response.sendError设置不同的错误码

    复制代码
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
  • ExceptionHandlerExceptionResolver 使用@ExceptionHandler注解的方法来进行异常解析

  • ResponseStatusExceptionResolver 解析有@ResponseStatus注解的异常

  • SimpleMappingExceptionResolver 通过配置的异常类和View的映射关系来解析异常

ExceptionHandlerExceptionResolver

我一般都是使用@ExceptionHandler注解,所以就以ExceptionHandlerExceptionResolver为例

所有的HandlerExceptionResolver实现类都是去重写resolveException方法

复制代码
// org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver#resolveException
@Override
public ModelAndView resolveException(
      HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
  // 判断当前HandlerExceptionResolver是否可以解析
   if (shouldApplyTo(request, handler)) {
     // 判断是否禁用缓存,如果禁用缓存的话,设置响应头
      prepareResponse(ex, response);
     // 具体的处理逻辑
      ModelAndView result = doResolveException(request, response, handler, ex);
      if (result != null) {
         
         // Explicitly configured warn logger in logException method.
         logException(ex, request);
      }
      return result;
   }
   else {
      return null;
   }
}
shouldApplyTo
复制代码
// org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver#shouldApplyTo
@Override
protected boolean shouldApplyTo(HttpServletRequest request, Object handler) {
   if (handler == null) {
      return super.shouldApplyTo(request, handler);
   }
   else if (handler instanceof HandlerMethod) {
      HandlerMethod handlerMethod = (HandlerMethod) handler;
      handler = handlerMethod.getBean();
      return super.shouldApplyTo(request, handler);
   }
   else {
      return false;
   }
}

// org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver#shouldApplyTo
protected boolean shouldApplyTo(HttpServletRequest request, Object handler) {
  if (handler != null) {
      // mappedHandlers是处理器的集合
      if (this.mappedHandlers != null && this.mappedHandlers.contains(handler)) {
    return true;
   }
      // mappedHandlerClasses是处理器类型的集合
   if (this.mappedHandlerClasses != null) {
    for (Class<?> handlerClass : this.mappedHandlerClasses) {
     if (handlerClass.isInstance(handler)) {
      return true;
     }
    }
   }
  }
  // Else only apply if there are no explicit handler mappings.
  return (this.mappedHandlers == null && this.mappedHandlerClasses == null);
 }
doResolveException

该方法是处理异常的实际方法

复制代码
protected final ModelAndView doResolveException(
      HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

   return doResolveHandlerMethodException(request, response, (HandlerMethod) handler, ex);
}

protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
   HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {
  // 找到处理异常的方法
  ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
  if (exceptionHandlerMethod == null) {
   return null;
  }
  // 设置argumentResolvers和returnValueHandlers
  exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
  exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);

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

  try {
   
   Throwable cause = exception.getCause();
   if (cause != null) {
    // Expose cause as provided argument as well
        // 执行exceptionHandler方法解析异常
    exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod);
   }
   else {
    // Otherwise, just the given exception as-is
    exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
   }
  }
  catch (Throwable invocationEx) {
   
   // Continue with default processing of the original exception...
   return null;
  }

  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());
   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;
  }
 }

https://zhhll.icu/2021/框架/springmvc/底层剖析/5.HandlerExceptionResolver/

本文由mdnice多平台发布

相关推荐
hnlucky10 分钟前
《Nginx + 双Tomcat实战:域名解析、静态服务与反向代理、负载均衡全指南》
java·linux·服务器·前端·nginx·tomcat·web
hnlucky11 分钟前
同时部署两个不同版本的tomcat要如何配置环境变量
java·服务器·http·tomcat·web
yngsqq1 小时前
netdxf—— CAD c#二次开发之(netDxf 处理 DXF 文件)
java·前端·c#
A了LONE1 小时前
h5的底部导航栏模板
java·前端·javascript
经典19922 小时前
spring boot 详解以及原理
java·spring boot·后端
星光54222 小时前
飞算JavaAI:给Java开发装上“智能引擎”的超级助手
java·开发语言
学习3人组2 小时前
JVM GC长暂停问题排查
java
R_AirMan2 小时前
深入浅出Redis:一文掌握Redis底层数据结构与实现原理
java·数据结构·数据库·redis
人生在勤,不索何获-白大侠3 小时前
day17——Java集合进阶(Collections、Map)
java·开发语言
程序员小羊!3 小时前
Java教程:JavaWeb ---MySQL高级
java·开发语言·mysql