Spring系列(七) Spring MVC 异常处理
2021-05-18 12:30
标签:ext.get packages scl fresh blog out 简单 https try Servlet规范规定了当web应用发生异常时必须能够指明, 并确定了该如何处理, 规定了错误信息应该包含的内容和展示页面的方式.(详细可以参考servlet规范文档) 所有的请求必然以某种方式转化为响应. 一.接口 该接口定义了Spring中该如何处理异常. 它只有一个方法 Spring 为该接口提供了若干实现类如下: 二. 这个类是Spring提供的默认实现, 用于将一些常见异常映射到特定的状态码. 这些状态码定义在接口 实际上, 接下来我们看 可以看到代码中使用了大量的分支语句, 实际上是将方法传入的异常类型通过instanceof运算符测试, 通过测试的转化为特定的异常. 并调用处理该异常的特定方法. 我们挑一个比如处理 上面分析了Spring默认的异常处理实现类 三. 如何使用? 只需要在异常上使用 四. 使用类似于普通的controller方法, 使用 无论是请求/npe1还是请求/npe2, 系统都会抛出异常, 并交给对应的处理程序 要了解其原理, 需要查看 可以看到在两个中文注释的地方, 其一是方法的开始部分获取到了异常的handler, 其二是调用这个handler的方法. 调用方法应该很好理解, 我们接下来查看方法 我们可以看到,它会首先查找controller中的方法, 如果找不到才去查找@ControllerAdvice注解的bean. 也就是说controller中的handler的优先级要高于advice. 上面我们了解了几个Exceptionresolver的使用, 并通过源代码简单看了他们各自处理的原理. 但这些Resolver如何加载我们还不知道, 接下来我们重点看下他们是如何加载进去的. 四. ExceptionResolver的加载 在本系列的上一篇Spring系列(六) Spring Web MVC 应用构建分析中, 我们大致提到了DispatcherServlet的启动调用关系如下: 整理下调用关系: 正是在 好了, 现在我们加载了应用程序中所有定义的Resolver. 当有请求到达时, doDispatch的代码, 只摘录出与本议题有关的. 从 从上面代码可以看到, Spring的异常解析器实现全部继承自接口 最后我们探究了异常处理器的加载和处理方式, 我们知道了其通过 Spring系列(七) Spring MVC 异常处理 标签:ext.get packages scl fresh blog out 简单 https try 原文地址:https://www.cnblogs.com/walkinhalo/p/9744656.htmlServlet传统异常处理
Spring MVC 处理方式
@ResponseStatus
注解可以映射某一异常到特定的HTTP状态码@ExceptionHandler
注解使其用来处理异常@ControllerAdvice
方式可以统一的方式处理全局异常Spring boot 方式
源码分析
HandlerExceptionResolver
resolveException()
, 接口源码如下: // 由对象实现的接口,这些对象可以解决在处理程序映射或执行期间引发的异常,在典型的情况下是错误视图。在应用程序上下文中,实现器通常被注册为bean。
// 错误视图类似于JSP错误页面,但是可以与任何类型的异常一起使用,包括任何已检查的异常,以及针对特定处理程序的潜在细粒度映射。
public interface HandlerExceptionResolver {
@Nullable
ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex);
}
HandlerExceptionResolverComposite 委托给其他HandlerExceptionResolver的实例列表
AbstractHandlerExceptionResolver 抽象基类
AbstractHandlerMethodExceptionResolver 支持HandlerMethod处理器的抽象基类
ExceptionHandlerExceptionResolver 通过 @ExceptionHandler 注解的方式实现的异常处理
DefaultHandlerExceptionResolver 默认实现, 处理spring预定义的异常并将其对应到错误码
ResponseStatusExceptionResolver 通过 @ResponseStatus 注解映射到错误码的异常
SimpleMappingExceptionResolver 允许将异常类映射到视图名
DefaultHandlerExceptionResolver
HttpServletResponse
中, 下面是几个状态码的代码片段public interface HttpServletResponse extends ServletResponse {
...
public static final int SC_OK = 200;
public static final int SC_MOVED_PERMANENTLY = 301;
public static final int SC_MOVED_TEMPORARILY = 302;
public static final int SC_FOUND = 302;
public static final int SC_UNAUTHORIZED = 401;
public static final int SC_INTERNAL_SERVER_ERROR = 500;
...
}
DefaultHandlerExceptionResolver
中并没有直接实现接口的resolveException
方法, 而是实现了抽象类AbstractHandlerExceptionResolver
的doResolveException()
方法, 后者则在实现了接口的方法中委托给抽象方法doResolveException
, 这个方法由子类去实现.AbstractHandlerExceptionResolver
的resolveException
方法代码如下:@Override
@Nullable
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
// 判断是否当前解析器可用于handler
if (shouldApplyTo(request, handler)) {
prepareResponse(ex, response);
ModelAndView result = doResolveException(request, response, handler, ex);
if (result != null) {
// Print warn message when warn logger is not enabled...
if (logger.isWarnEnabled() && (this.warnLogger == null || !this.warnLogger.isWarnEnabled())) {
logger.warn("Resolved [" + ex + "]" + (result.isEmpty() ? "" : " to " + result));
}
// warnLogger with full stack trace (requires explicit config)
logException(ex, request);
}
return result;
}
else {
return null;
}
}
DefaultHandlerExceptionResolver
实现的doResolveException
方法. 代码如下;@Override
@Nullable
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 NoHandlerFoundException) {
return handleNoHandlerFoundException(
(NoHandlerFoundException) ex, request, response, handler);
}
.....
}
catch (Exception handlerEx) {
if (logger.isWarnEnabled()) {
logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", handlerEx);
}
}
return null;
}
NoHandlerFoundException
这个异常类的方法, 这个方法将异常映射为404错误.protected ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex,
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
pageNotFoundLogger.warn(ex.getMessage());
response.sendError(HttpServletResponse.SC_NOT_FOUND); //设置为404错误
return new ModelAndView(); //返回个空视图
}
DefaultHandlerExceptionResolver
.它处理的异常是Spring预定义的几种常见异常, 它将异常对应到HTTP的状态码. 而对于不属于这些类型的其他异常, 我们可以使用ResponseStatusExceptionResolver
来处理, 将其对应到HTTP状态码.ResponseStatusExceptionResolver
@GetMapping("/responseStatus")
@ResponseBody
public String responseStatus() throws MyException {
throw new MyException();
}
@ResponseStatus(code = HttpStatus.BAD_GATEWAY)
public class MyException extends Exception{}
@ResponseStatus
注解即可将特定的自定义异常对应到Http的状态码.ExceptionHandlerExceptionResolver
@ExceptionHandler
注解的方法将作为处理该注解参数中异常的handler. 比如, 在一个controller中, 我们定义一个处理NPE的异常处理handler方法, 可以用来处理该controller中抛出的NPE. 代码如下: @GetMapping("/npe1")
@ResponseBody
public String npe1() throws NullPointerException {
throw new NullPointerException();
}
@GetMapping("/npe2")
@ResponseBody
public String npe2() throws NullPointerException {
throw new NullPointerException();
}
@ExceptionHandler(value = {NullPointerException.class})
@ResponseBody
public String npehandler(){
return "test npe handler";
}
npehandler
去处理. 使用@ExceptionHandler(value = {NullPointerException.class})
注解的方法可以处理本controller范围内的所有方法排除的npe异常, 如果要将其作为应用中所有controller的异常处理器, 就要将其定义在@ControllerAdvice
注解的类中.@ControllerAdvice
public class ControllerAdvicer {
@ExceptionHandler(value = {NullPointerException.class})
@ResponseBody
public String npehandler(){
return "test npe handler in advice";
}
}
ExceptionHandlerExceptionResolver
中的方法doResolveHandlerMethodException
@Override
@Nullable
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) {
// 获取异常对用的处理器, 就是@ExceptionHandler注解的方法包装, 注意参数handlerMethod, 在方法内部, 它将用来获取所在Controller的信息
ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
if (exceptionHandlerMethod == null) {
return null;
}
if (this.argumentResolvers != null) {
exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
}
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();
// 调用异常处理handler的方法.
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;
}
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
getExceptionHandlerMethod
.// 找到给定异常对应的@ExceptionHandler注解方法, 默认先在controller类的继承结构中查找, 否则继续在@ControllerAdvice注解的 bean中查找.
@Nullable
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(
@Nullable HandlerMethod handlerMethod, Exception exception) {
Class> handlerType = null;
if (handlerMethod != null) {
// Local exception handler methods on the controller class itself.
// To be invoked through the proxy, even in case of an interface-based proxy.
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);
}
// For advice applicability check below (involving base packages, assignable types
// and annotation presence), use target class instead of interface-based proxy.
if (Proxy.isProxyClass(handlerType)) {
handlerType = AopUtils.getTargetClass(handlerMethod.getBean());
}
}
// 在@ControllerAdvice注解的类中遍历查找
for (Map.Entry
DispatcherServlet
initHandlerMappings FrameworkServlet
initWebApplicationContext HttpServletBean
init GenericServlet
init(ServletConfig config)
最后的GenericServlet
是servlet Api的.initStrategies
方法中, DispatcherServlet
做了启动的一系列工作, 除了initHandlerMappings
还可以看到一个initHandlerExceptionResolvers
的方法, 其源码如下:// 初始化HandlerExceptionResolver, 如果没有找到任何命名空间中定义的bean, 默认没有任何resolver
private void initHandlerExceptionResolvers(ApplicationContext context) {
this.handlerExceptionResolvers = null;
if (this.detectAllHandlerExceptionResolvers) {
// 找到所有ApplicationContext中定义的 HandlerExceptionResolvers 包括在上级上下文中.
Map
DispatcherServlet
的doDispatch
方法使用请求特定的handler处理, 当handler发生异常时, 变量dispatchException
的值赋值为抛出的异常, 并委托给方法processDispatchResult
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
....
try {
ModelAndView mv = null;
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
}catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we‘re processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
....
}
// 处理handler的结果
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
@Nullable Exception exception) throws Exception {
boolean errorView = false;
// 异常处理
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);
}
}
// handler是否返回了view
if (mv != null && !mv.wasCleared()) {
render(mv, request, response);
if (errorView) {
WebUtils.clearErrorRequestAttributes(request);
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace("No view rendering, null ModelAndView returned.");
}
}
if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
// Concurrent handling started during a forward
return;
}
if (mappedHandler != null) {
mappedHandler.triggerAfterCompletion(request, response, null);
}
}
processDispatchResult
方法中可以看到, 如果参数exception
不为null, 则会处理异常, 对于ModelAndViewDefiningException
类型的异常单独处理, 对于其他类型的异常, 转交给processHandlerException
方法处理, 这个方法就是异常处理逻辑的核心.@Nullable
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
@Nullable Object handler, Exception ex) throws Exception {
// Success and error responses may use different content types
request.removeAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
// 使用注册的Resolver处理
ModelAndView exMv = null;
if (this.handlerExceptionResolvers != null) {
for (HandlerExceptionResolver resolver : this.handlerExceptionResolvers) {
exMv = resolver.resolveException(request, response, handler, ex);
if (exMv != null) {
break;
}
}
}
if (exMv != null) {
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);
}
}
if (logger.isTraceEnabled()) {
logger.trace("Using resolved error view: " + exMv, ex);
}
if (logger.isDebugEnabled()) {
logger.debug("Using resolved error view: " + exMv);
}
WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
return exMv;
}
throw ex;
}
this.handlerExceptionResolvers
就是在程序启动时初始化注册的, spring通过遍历Resolver列表的方式处理异常, 如果返回结果不为null, 说明处理成功, 就跳出循环.总结
ResponseStatusExceptionResolver
, 上面我们详细了解了该接口在Spring中的几种实现, 比如处理预定义异常的DefaultHandlerExceptionResolver
, 可以映射异常到状态码的ResponseStatusExceptionResolver
, 还有功能更为强大的ExceptionHandlerExceptionResolver
. 同时也简单了解了其使用方式,使用@ExceptionHandler
来将方法标记为异常处理器, 结合@ControllerAdvice
处理全局异常.DispatcherServlet
的初始化方法initHandlerMappings
完成加载器列表的注册初始化, 并且在具体处理请求的doDispatch
中检测异常, 最终processDispatchResult
方法委托给processHandlerException
, 该方法循环注册的异常处理器列表完成处理过程.