springmvc支持服务端在处理业务逻辑过程中出现异常的时候可以配置相应的ModelAndView对象返回给客户端,本文介绍springmvc默认的几种HandlerExceptionResolver类

实际应用

springmvc的xml配置化-Exception配置

<bean id="exceptionHandler" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--设置默认返回viewName,通常与freemarker引擎搭配使用-->
<property name="defaultErrorView" value="error/defaultError" />
<!--设置默认返回response status-->
<property name="defaultStatusCode" value="500" /> <!--配置相应的异常类与viewName的映射-->
<property name="exceptionMappings">
<props>
<prop key="SessionTimeoutException">redirect:../login.html</prop>
<prop key="AuthenticationException">error/403</prop>
</props>
</property>
</bean>

以上的配置会对出SessionTimeoutException异常则跳转至login页面,对AuthenticationException异常则跳转至403页面,对其他的异常则默认跳转至defaultError页面呈现并返回500的错误码

HandlerExceptionResolver-异常解析接口

接口内只有一个方法resolveException(),通过解析异常查询配置以得到符合条件的ModelAndView对象

	/**
* 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
*/
ModelAndView resolveException(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);

AbstractHandlerExceptionResolver-异常解析抽象基类

所有的spring内置异常解析类都继承于此,直奔主题看resolveException()方法

	@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
//判断是否需要解析
if (shouldApplyTo(request, handler)) {
//此处一般是判断内部属性preventResponseCaching是否为true,是则设置响应包头cache-control:no-store
prepareResponse(ex, response);
//使用模板方法doResolveException()方法供子类实现
ModelAndView result = doResolveException(request, response, handler, ex);
if (result != null) {
//日志打印一发
logException(ex, request);
}
return result;
}
else {
return null;
}
}

附带着分析下shouldApplyTo()方法

	/**
**可以配置mappedHandlers和mappedHandlerClasses属性来特定匹配
**默认情况下两者都为空则直接返回true,表明对所有的handler都进行异常解析
*/
protected boolean shouldApplyTo(HttpServletRequest request, Object handler) {
//此处的handler一般为bean对象
if (handler != null) {
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;
}
}
}
}
// Else only apply if there are no explicit handler mappings.
return (this.mappedHandlers == null && this.mappedHandlerClasses == null);
}

1. SimpleMappingExceptionResolver-异常映射实现类

比较简单的实现类,可以配绑定viewName和exception以完成简单的异常映射视图页面

	protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) { // Expose ModelAndView for chosen error view.
String viewName = determineViewName(ex, request);
if (viewName != null) {
//如果配置了statusCodes属性,则对此异常的状态码进行设置
Integer statusCode = determineStatusCode(request, viewName);
if (statusCode != null) {
applyStatusCodeIfPossible(request, response, statusCode);
}
//创建ModelAndView对象
return getModelAndView(viewName, ex, request);
}
else {
return null;
}
}

针对以上的源码我们分两步去简单分析下

SimpleMappingExceptionResolver#determineViewName()-找寻viewName

	protected String determineViewName(Exception ex, HttpServletRequest request) {
String viewName = null;
//判断异常是否属于excludeExceptions集合内,是则直接返回null
if (this.excludedExceptions != null) {
for (Class<?> excludedEx : this.excludedExceptions) {
if (excludedEx.equals(ex.getClass())) {
return null;
}
}
}
// Check for specific exception mappings.
// 从exceptionMappings集合内根据exception获取到相应的viewName
if (this.exceptionMappings != null) {
viewName = findMatchingViewName(this.exceptionMappings, ex);
}
//当exceptionMappings集合内不存在指定的exception但是默认视图指定则直接返回默认视图
if (viewName == null && this.defaultErrorView != null) {
viewName = this.defaultErrorView;
}
return viewName;
}
  • excludedExceptions集合可以过滤指定的exception,对其不进行解析直接返回null。可配置

  • exceptionMappings绑定了exception与viewName的关系,如果在其集合内没找到相应的viewName,但是defaultErrorView属性指定,则会直接返回defaultErrorView对应的视图

SimpleMappingExceptionResolver#getModelAndView()-创建ModelAndView对象

	protected ModelAndView getModelAndView(String viewName, Exception ex) {
ModelAndView mv = new ModelAndView(viewName);
//exceptionAttribute默认为exception
if (this.exceptionAttribute != null) {
//将exception信息添加到model中
mv.addObject(this.exceptionAttribute, ex);
}
return mv;
}

主要就是将exceptionAttribute对应的参数值默认为exception属性添加到视图对象的model中

2. ResponseStatusExceptionResolver-响应状态异常解析类

主要是解析带有@ResponseStatus的异常类,将其中的异常信息描述直接返回给客户端

	@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
//获取相应类上的注解@ResponseStatus
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class);
if (responseStatus != null) {
try {
return resolveResponseStatus(responseStatus, request, response, handler, ex);
}
catch (Exception resolveEx) {
logger.warn("Handling of @ResponseStatus resulted in Exception", resolveEx);
}
}
else if (ex.getCause() instanceof Exception) {
ex = (Exception) ex.getCause();
//递归
return doResolveException(request, response, handler, ex);
}
return null;
}

ResponseStatusExceptionResolver#resolveResponseStatus()-返回异常信息给客户端

读取@ResponseStatus注解信息,返回异常内容给客户端

	protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) throws Exception {
//状态码
int statusCode = responseStatus.code().value();
//异常原因描述
String reason = responseStatus.reason();
if (this.messageSource != null) {
reason = this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale());
}
//通过response对象直接返回错误信息给客户端
if (!StringUtils.hasLength(reason)) {
response.sendError(statusCode);
}
else {
//通过response对象直接返回错误信息给客户端
response.sendError(statusCode, reason);
}
return new ModelAndView();
}

3. DefaultHandlerExceptionResolver-springmvc默认的异常解析处理

源码就不公布了,读者可自行去查询,基本都是调用response的sendError()方法返回错误信息给客户端。本文对其中的异常归下类

  1. 请求方式异常
  • HttpRequestMethodNotSupportedException-服务端不支持相应的请求方法
  • HttpMediaTypeNotSupportedException/HttpMediaTypeNotAcceptableException-服务端/客户端不支持相应的mediaType,比如application/json
  • MissingPathVariableException-@PathVaribale指定参数请求中不包含
  • MissingServletRequestParameterException/ServletRequestBindingException-请求参数绑定错误
  • MethodArgumentNotValidException-@Valid注解指定的参数校验失败
  • AsyncRequestTimeoutException-异步请求超时
  1. 消息内容异常
  • ConversionNotSupportedException-服务端找寻不到相应的Convert对象来解析javabean
  • TypeMismatchException-设置javabean属性类型出错
  • HttpMessageNotReadableException/HttpMessageNotWritableException-消息内容不可读/不可写
  • MissingServletRequestPartException-文件上传类错误,可能请求没有multipart/form-data或者服务不支持文件上传
  • NoHandlerFoundException-handler处理器没有找到,即可能没有对应的请求处理供响应

4. ExceptionHandlerExceptionResolver-处理handlerMethod对象过程中的异常

具体的逻辑本文则不展开了,简述下其中的逻辑:

当处理handlerMethod业务逻辑过程中出现了异常,则此解析器

  1. 尝试从handlerMethod所在的class类去找寻是否含有@ExceptionHandler注解的方法

  2. 判断@ExceptionHandler指定的exception类与产生的异常一致,一致则执行相应的方法,当有多个@ExceptionHandler(value),则默认采用第一个

  3. 当上述在class类找寻不到则尝试判断class类是否含有@ControllerAdvice注解,有则按照上述第一二步的步骤再次找寻@ControllerAdvice指定的类

小结

springmvc开放了对异常也可以包装成页面显示的功能,通过本文的简单分析可以帮助博主和读者更好的理解springmvc对异常的处理

SpringMVC源码情操陶冶-AbstractHandlerExceptionResolver的更多相关文章

  1. SpringMVC源码情操陶冶-DispatcherServlet简析(二)

    承接前文SpringMVC源码情操陶冶-DispatcherServlet类简析(一),主要讲述初始化的操作,本文将简单介绍springmvc如何处理请求 DispatcherServlet#doDi ...

  2. SpringMVC源码情操陶冶-FreeMarker之web配置

    前言:本文不讲解FreeMarkerView视图的相关配置,其配置基本由FreeMarkerViewResolver实现,具体可参考>>>SpringMVC源码情操陶冶-ViewRe ...

  3. SpringMVC源码情操陶冶-DispatcherServlet

    本文对springmvc核心类DispatcherServlet作下简单的向导,方便博主与读者查阅 DispatcherServlet-继承关系 分析DispatcherServlet的继承关系以及主 ...

  4. SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

    mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...

  5. SpringMVC源码情操陶冶-DispatcherServlet父类简析

    阅读源码有助于陶冶情操,本文对springmvc作个简单的向导 springmvc-web.xml配置 <servlet> <servlet-name>dispatch< ...

  6. SpringMVC源码情操陶冶-DispatcherServlet类简析(一)

    阅读源码有利于陶冶情操,此文承接前文SpringMVC源码情操陶冶-DispatcherServlet父类简析 注意:springmvc初始化其他内容,其对应的配置文件已被加载至beanFactory ...

  7. SpringMVC源码情操陶冶-HandlerAdapter适配器简析

    springmvc中对业务的具体处理是通过HandlerAdapter适配器操作的 HandlerAdapter接口方法 列表如下 /** * Given a handler instance, re ...

  8. SpringMVC源码情操陶冶-AbstractHandlerMethodMapping

    承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,本文将介绍如何注册HandlerMethod对象作为handler 类结构瞧一瞧 public abstract ...

  9. SpringMVC源码情操陶冶-AbstractUrlHandlerMapping

    承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,前文主要讲解了如何获取handler处理对象,本文将针对beanName注册为handler对象作下解析 Abst ...

随机推荐

  1. 你是否也在学习ES6 Promise时遇到过这个问题?

    背景 周末闲来无事,随便翻看了一下阮一峰老师的<ES6 标准入门>第2版,ps:之前在阮一峰老师的官网看过电子版,感觉干货满满,所以就买了纸质版:当看到第16章第4节 'Promise.p ...

  2. weather API 天气api接口 收集整理

    腾讯 http://sou.qq.com/online/get_weather.php?callback=Weather&city=南京 中国天气-weather.com.cn http:// ...

  3. fetch()的用法

    发起获取资源请求的我们一般都会用AJAX技术,最近在学习微信小程序的时候,用到了fetch()方法. fetch()方法用于发起获取资源的请求.它返回一个promise,这个promise会在请求响应 ...

  4. python 标准库 -- pickle & cPickle

    pickle & cPickle pickle 和 cPickle 除了导入名称不一样之外, 使用方法, 均一样. pickle 导入 import pickle cPickle 导入 imp ...

  5. [项目记录]一个.net下使用HAP实现的吉大校园通知网爬虫工具:OAWebScraping

    第一章 简介 本文主要介绍了在.NET下利用优秀的HTML解析组件HtmlAgilityPack开发的一个吉林大学校内通知oa.jlu.edu.cn的爬取器.尽管.Net下解析HTML文件有很多种选择 ...

  6. .net 4.0 中的特性总结(五):并行编程

    1.任务并行库 从 .NET Framework 4 开始,TPL 是编写多线程代码和并行代码的首选方法. 但是,并不是所有代码都适合并行化:例如,如果某个循环在每次迭代时只执行少量工作,或它在很多次 ...

  7. asp.net web api实现图片点击式图片验证码

    现在验证码的形式越来越丰富,今天要实现的是在点击图片中的文字来进行校验的验证码,如图 这种验证码验证是验证鼠标是否选中了图片中文字的位置,以及选择的顺序,产生验证码的时候可以提供一组底图,然后随机获取 ...

  8. 警惕System.Environment.CurrentDirectory 获取当前目录

    最近工作中,要做个客户端提醒的小工具:winform程序自然少不了要读取和应用程序同一个目录的配置文件(不是exe.config文件): 要读取当前应用程序所在目录我立马想到了System.Envir ...

  9. USACO Ordered Fractions

    首先看一下题目 Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less t ...

  10. 8.Smarty的条件判断语句的写法

    {if $newObj eq 'a'} welcome a {elseif $a eq 'b'} welcome b {else} welcome others {/if}