SpringMVC异常体系分析
1、源头
Springmvc中对异常的处理过程分析:
org.springframework.web.servlet.DispatcherServlet#doDispatch
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
// 如果在调用handler方法的时候出现了异常,那么将会被catch住,然后获取得到异常!
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);
}
// 如果再次出现异常,那么这里仍然会执行triggerAfterCompletion。可以看到下面如果有异常就会来执行
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
总结一些这里的流程:
A、首先根据request来找到对应的handler;
B、根据handler来找到对应的adapter;
C、通过adapter执行拦截器的前置处理方法,为handler来做一个准备;
- 如果执行结果为false,那么直接不向下执行了;也就是说都没有来得及执行到我们自己在controller中写的方法;
- 如果执行结果为true,那么接着执行。可能这个时候才会执行到handler方法;
D、真正执行目标方法并将返回值封装到ModelAndView中来;
E、在上面的try....catch代码块中可能出现异常
- 没有出现异常,那么将会执行拦截器的后置处理方法(这个地方也可能会出现异常!)
- 出现异常
都会走到对转发结果的处理。实际上这里只会做两个事情:1、判断是否是跳转页面的;2、是否存在着异常,如果存在着异常,那么又将会来如何进行处理。如果异常都没有来进行处理。
然后再去执行拦截器的最终处理方法。可以看到方法的执行时机是在render渲染方法之后。这个是视图渲染,视图渲染之后也就到了即将给页面上进行响应的时机。所以这里是收尾的工作,所有的事情在这里都已经做完了,所以以后的ModelAndView都不会再做改变了。(暂且可以这样子来进行理解)
2、processDispatchResult
那么看一下processDispatchResult方法的处理:
/**
* Handle the result of handler selection and handler invocation, which is
* either a ModelAndView or an Exception to be resolved to a ModelAndView.
*/
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
@Nullable Exception exception) throws Exception {
boolean errorView = false;
// 如果是从handler抛出来的异常,那么会走这里。
if (exception != null) {
// 如果是这个异常,那么走下面的逻辑
if (exception instanceof ModelAndViewDefiningException) {
logger.debug("ModelAndViewDefiningException encountered", exception);
mv = ((ModelAndViewDefiningException) exception).getModelAndView();
}
else {
// 获取得到当前的handler
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.isTraceEnabled()) {
logger.trace("No view rendering, null ModelAndView returned.");
}
}
if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
// Concurrent handling started during a forward
return;
}
if (mappedHandler != null) {
// Exception (if any) is already handled..
// 如果有异常的话,这里已经处理完了
// 还是会来做一些收尾性质的工作。一般说来,这里不会再来动reqeust和response
mappedHandler.triggerAfterCompletion(request, response, null);
}
}
所以可以看到来对异常进行处理的方法:
@Nullable
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) throws Exception {
request.removeAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
ModelAndView exMv = null;
// 遍历获取得到所有的异常解析器。
// 如果没有自定义的异常处理器,那么就是框架中提供的几个来进行处理
// 当然也可以自己来定义异常解析器,这里有对应的接口,只需要来做一个实现即可。在下面将会来做一个异常解析器
if (this.handlerExceptionResolvers != null) {
Iterator var6 = this.handlerExceptionResolvers.iterator();
while(var6.hasNext()) {
HandlerExceptionResolver resolver = (HandlerExceptionResolver)var6.next();
// 遍历获取得到每个异常解析器,来对异常来进行处理。返回解析后的modelAndView视图信息
exMv = resolver.resolveException(request, response, handler, ex);
// 只有不为空的时候,Springmvc认为才是可以来对其进行处理的异常解析器
if (exMv != null) {
break;
}
}
}
if (exMv != null) {
// 为空,直接返回为null
if (exMv.isEmpty()) {
request.setAttribute(EXCEPTION_ATTRIBUTE, ex);
return null;
} else {
// 没有包含视图信息
if (!exMv.hasView()) {
String defaultViewName = this.getDefaultViewName(request);
if (defaultViewName != null) {
exMv.setViewName(defaultViewName);
}
}
if (this.logger.isTraceEnabled()) {
this.logger.trace("Using resolved error view: " + exMv, ex);
} else if (this.logger.isDebugEnabled()) {
this.logger.debug("Using resolved error view: " + exMv);
}
WebUtils.exposeErrorRequestAttributes(request, ex, this.getServletName());
// 将ModelAndView进行返回
return exMv;
}
} else {
throw ex;
}
}
3、异常体系图
所以重点就在于如何对异常来进行解析并返回对应的ModelAndView的。
public interface HandlerExceptionResolver {
@Nullable
ModelAndView resolveException(HttpServletRequest var1, HttpServletResponse var2, @Nullable Object var3, Exception var4);
}
接下来就来到了重点接口中,那么查看这个接口中只有一个方法,方法名称表示的是用来处理异常。那么看一下整个体系图:
从继承体系中可以看到AbstractHandlerExceptionResolver整个类的处理方式是整个异常处理解析器的父类,那么看一下父类中是如何来进行处理的:
@Nullable
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
// 首先来判断该异常解析能够来处理当前的异常
// 如果不能够处理,那么
if (!this.shouldApplyTo(request, handler)) {
return null;
} else {
this.prepareResponse(ex, response);
// 如果能够处理当前handler抛出的异常信息
ModelAndView result = this.doResolveException(request, response, handler, ex);
if (result != null) {
if (this.logger.isDebugEnabled() && (this.warnLogger == null || !this.warnLogger.isWarnEnabled())) {
this.logger.debug("Resolved [" + ex + "]" + (result.isEmpty() ? "" : " to " + result));
}
this.logException(ex, request);
}
return result;
}
}
那么看一下对应的shouldApplyTo方法:
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {
if (handler != null) {
// 判断所有的handler中是否包含了当前的handler。如果包含了,那么执行下一步
if (this.mappedHandlers != null && this.mappedHandlers.contains(handler)) {
return true;
}
if (this.mappedHandlerClasses != null) {
Class[] var3 = this.mappedHandlerClasses;
int var4 = var3.length;
for(int var5 = 0; var5 < var4; ++var5) {
Class<?> handlerClass = var3[var5];
if (handlerClass.isInstance(handler)) {
return true;
}
}
}
}
return this.mappedHandlers == null && this.mappedHandlerClasses == null;
}
异常处理器能够来进行处理,如果可以,那么返回true;如果不能,那么返回false
这里涉及到了两个对象:mappedHandlers 和 mappedHandlerClasses:
- mappedHandlers:存储的是处理器对象(Controller 或者 Controller 中的方法)
- mappedHandlerClasses:存储的是处理器的 Class。
我们在配置异常解析器的时候可以配置这两个对象,进而实现该异常处理器只为某一个处理器服务,但是一般来说没这种需求,所以大家仅做了解即可。
如果开发者一开始配置了 mappedHandlers 或者 mappedHandlerClasses,则用这两个和处理器去比较,否则就直接返回 true,表示支持该异常处理。
所以紧接着来到了父类的doResolveException,对异常做处理的方法中来。
@Nullable
protected abstract ModelAndView doResolveException(HttpServletRequest var1, HttpServletResponse var2, @Nullable Object var3, Exception var4);
发现父类中是一个抽象方法,那么就说明了这个方法是一个模板方法,交付给各自的子类来进行处理。所以下面就来到了各自的子类中是如何来进行处理的。
那么断点打一下,看看对应的执行顺序:
那么依次来看每个异常处理器的处理方式:
org.springframework.boot.web.servlet.error.DefaultErrorAttributes#resolveException
对应的处理方式:
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
this.storeErrorAttributes(request, ex);
return null;
}
这里是直接将异常信息进行了存储,然后返回null。但是根据上面的判断,如果是null,那么将会进入到下一轮循环中去
org.springframework.web.servlet.handler.HandlerExceptionResolverComposite#resolveException
顺便说一下,看看这里的继承方式:
public class HandlerExceptionResolverComposite implements HandlerExceptionResolver, Ordered {....}
看一下对应的加载顺序等等操作。
那么接着看对应的处理方式,因为这个是一个组合的,而且里面有三个异常处理器。所以下面接着看每一个是如何来进行处理的。
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver#resolveException
这个是父类中的方法,父类中只是来做了一个处理方式,但是因为这里的resolveException方法是抽象的,那么这里将会挨个去调用子类中独有的方法来进行操作。
public ModelAndView resolveException(
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
if (this.resolvers != null) {
for (HandlerExceptionResolver handlerExceptionResolver : this.resolvers) {
ModelAndView mav = handlerExceptionResolver.resolveException(request, response, handler, ex);
if (mav != null) {
return mav;
}
}
}
return null;
}
然后看一下调用:
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);
if (result != null) {
// Print debug message when warn logger is not enabled.
if (logger.isDebugEnabled() && (this.warnLogger == null || !this.warnLogger.isWarnEnabled())) {
logger.debug("Resolved [" + ex + "]" + (result.isEmpty() ? "" : " to " + result));
}
// Explicitly configured warn logger in logException method.
logException(ex, request);
}
return result;
}
else {
return null;
}
}
首先调用 shouldApplyTo 方法判断当前解析器是否可以处理传入的处理器(handler)所抛出的异常,如果不支持,则直接返回 null,这个异常将交给下一个 HandlerExceptionResolver 去处理。
调用 prepareResponse 方法处理 response。(设置缓存为不缓存)
调用 doResolveException 方法实际处理异常,这是一个模版方法,具体的实现在子类中。
调用 logException 方法记录异常日志信息。
记录异常日志没啥好说的,doResolveException 则是一个空的模版方法,所以这里对我们来说主要就是两个方法:shouldApplyTo 和 prepareResponse,我们分别来看。
shouldApplyTo 方法:
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {
if (handler != null) {
// handler并来判断是否包含当前的handler。
// 所以这里就不应该是所有的handler
if (this.mappedHandlers != null && this.mappedHandlers.contains(handler)) {
return true;
}
// 如果上面的handler排除了,将会走到这个方法来
if (this.mappedHandlerClasses != null) {
for (Class<?> handlerClass : this.mappedHandlerClasses) {
if (handlerClass.isInstance(handler)) {
return true;
}
}
}
}
return !hasHandlerMappings();
}
这里涉及到了两个对象:mappedHandlers 和 mappedHandlerClasses:
mappedHandlers:存储的是处理器对象(Controller 或者 Controller 中的方法)
mappedHandlerClasses:存储的是处理器的 Class。
我们在配置异常解析器的时候可以配置这两个对象,进而实现该异常处理器只为某一个处理器服务,但是一般来说没这种需求,所以大家仅做了解即可。
如果开发者一开始配置了 mappedHandlers 或者 mappedHandlerClasses,则用这两个和处理器去比较,否则就直接返回 true,表示支持该异常处理。
这里只是一个简单说明,一般来说不会这样子来进行使用。所以这里我们默认返回的是true。
doResolveException
protected final ModelAndView doResolveException(
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
HandlerMethod handlerMethod = (handler instanceof HandlerMethod ? (HandlerMethod) handler : null);
return doResolveHandlerMethodException(request, response, handlerMethod, ex);
}
我们的Handler都是这个HandlerMethod类型的
doResolveException这个方法是父类的,那么看一下每个子类中的实现:
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#doResolveHandlerMethodException
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) {
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("Using @ExceptionHandler " + 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 (or its cause) is unintended here,
// probably an accident (e.g. failed assertion or the like).
if (invocationEx != exception && invocationEx != exception.getCause() && logger.isWarnEnabled()) {
logger.warn("Failure in @ExceptionHandler " + 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<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
}
return mav;
}
}
1、首先查找到带有 @ExceptionHandler和@controllerAdvice注解的方法,封装成一个 ServletInvocableHandlerMethod 对象(关于 ServletInvocableHandlerMethod 对象;
2、如果找到了对应的方法,则为 exceptionHandlerMethod 配置参数解析器、视图解析器等。因为也要对参数和返回值来做处理
3、获取得到指定的异常,交给当前对象来进行处理;
4、如果当前类无法处理异常,则继续抛出,交给下一个异常处理器来进行处理;因为无法处理的时候,会继续抛出,抛出会返回null,而如果modelandview为null,将会继续下一个异常处理器来解决。
所以我们就看一下ExceptionHandlerExceptionResolver如何来进行判断的。
之前已经说过了,找到了@ExceptionHandler和@controllerAdvice注解的类和对应的方法
public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer,
Object... providedArgs) throws Exception {
Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
setResponseStatus(webRequest);
if (returnValue == null) {
if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) {
disableContentCachingIfNecessary(webRequest);
mavContainer.setRequestHandled(true);
return;
}
}
else if (StringUtils.hasText(getResponseStatusReason())) {
mavContainer.setRequestHandled(true);
return;
}
mavContainer.setRequestHandled(false);
Assert.state(this.returnValueHandlers != null, "No return value handlers");
try {
this.returnValueHandlers.handleReturnValue(
returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
}
catch (Exception ex) {
if (logger.isTraceEnabled()) {
logger.trace(formatErrorForReturnValue(returnValue), ex);
}
throw ex;
}
}
1、确定参数值和返回值后,得到返回值。如果返回值为空,那么设置当前请求结束了;如果返回值不为空,那么继续向下执行;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
public void defaultExceptionHandler(HttpServletRequest request,Exception e){
System.out.println(request.getRequestURI());
System.out.println(e);
}
}
响应完成之后,页面上什么都没有显示。
2、判断ResponseStatus中的reason是否是有值的,如果有值,也结束请求,直接返回。
这里就让我想起来了我犯的一个错误:
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 能够处理这种请求
* @return
*/
@ExceptionHandler(value = Exception.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "响应成功")
public String defaultExceptionHandler(HttpServletRequest request,Exception e){
System.out.println(request.getRequestURI());
System.out.println(e);
return "success";
}
}
正是因为加了这个reason,所以我的页面上直接把错误抛出来了。
找了这个问题找了半天,这里记录一下。
所以对于ExceptionHandlerExceptionResolver这个异常处理器来说,使用的时候注意一点,必须要有@ExceptionHandler和@controllerAdvice或者有其衍生注解。如:@ExceptionHandler和@RestControllerAdvice或者是@ExceptionHandler和@RestControllerAdvice
如果有@ResponseStatus注解,那么一定注意,不要写reason,否则写了等于没有对异常来做处理。
最佳实践:
@ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseVO<String> defaultExceptionHandler(HttpServletRequest request,Exception e){
Log.error("requestURI:{} ",request.getRequestURI(),e);
return ResponseUtil.error();
}
3、对返回值做处理,如果是成功的话,那么将直接new ModelAndView()进行返回,那么对于参数解析器的处理将会直接处理成功。
否则返回null,继续下一个参数处理器的处理。
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#doResolveException
从名字上可以看到是默认的异常处理器,用来处理一些常见的异常类型
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 HttpMediaTypeNotAcceptableException) {
return handleHttpMediaTypeNotAcceptable(
(HttpMediaTypeNotAcceptableException) ex, request, response, handler);
}
else if (ex instanceof MissingPathVariableException) {
return handleMissingPathVariable(
(MissingPathVariableException) ex, request, response, handler);
}
else if (ex instanceof MissingServletRequestParameterException) {
return handleMissingServletRequestParameter(
(MissingServletRequestParameterException) ex, request, response, handler);
}
else if (ex instanceof ServletRequestBindingException) {
return handleServletRequestBindingException(
(ServletRequestBindingException) ex, request, response, handler);
}
else if (ex instanceof ConversionNotSupportedException) {
return handleConversionNotSupported(
(ConversionNotSupportedException) ex, request, response, handler);
}
else if (ex instanceof TypeMismatchException) {
return handleTypeMismatch(
(TypeMismatchException) ex, request, response, handler);
}
else if (ex instanceof HttpMessageNotReadableException) {
return handleHttpMessageNotReadable(
(HttpMessageNotReadableException) ex, request, response, handler);
}
else if (ex instanceof HttpMessageNotWritableException) {
return handleHttpMessageNotWritable(
(HttpMessageNotWritableException) ex, request, response, handler);
}
else if (ex instanceof MethodArgumentNotValidException) {
return handleMethodArgumentNotValidException(
(MethodArgumentNotValidException) ex, request, response, handler);
}
else if (ex instanceof MissingServletRequestPartException) {
return handleMissingServletRequestPartException(
(MissingServletRequestPartException) ex, request, response, handler);
}
else if (ex instanceof BindException) {
return handleBindException((BindException) ex, request, response, handler);
}
else if (ex instanceof NoHandlerFoundException) {
return handleNoHandlerFoundException(
(NoHandlerFoundException) ex, request, response, handler);
}
else if (ex instanceof AsyncRequestTimeoutException) {
return handleAsyncRequestTimeoutException(
(AsyncRequestTimeoutException) ex, request, response, handler);
}
}
catch (Exception handlerEx) {
if (logger.isWarnEnabled()) {
logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", handlerEx);
}
}
return null;
}
可以看到,这里实际上就是根据不同的异常类型,然后调用不同的类去处理该异常。这里相关的处理都比较容易,以 HttpRequestMethodNotSupportedException 为例,异常处理就是对 response 对象做一些配置,如下:
protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
String[] supportedMethods = ex.getSupportedMethods();
if (supportedMethods != null) {
response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
}
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
return new ModelAndView();
}
配置响应头,然后 sendError,最后返回一个空的 ModelAndView 对象。
继续到下一个异常解析器:
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#doResolveException
这个从名字上看就是对ResponseStatus来做处理的异常解析器:
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 status = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class);
if (status != null) {
return resolveResponseStatus(status, request, response, handler, ex);
}
if (ex.getCause() instanceof Exception) {
return doResolveException(request, response, handler, (Exception) ex.getCause());
}
}
catch (Exception resolveEx) {
if (logger.isWarnEnabled()) {
logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", resolveEx);
}
}
return null;
}
可以看到,首先判断异常类型是不是 ResponseStatusException,如果是,则直接调用 resolveResponseStatusException 方法进行异常信息处理,如果不是,则去查找到异常类上的 @ResponseStatus 注解,并从中查找出相关的异常信息,然后调用 resolveResponseStatus 方法进行处理。
可以看到,ResponseStatusExceptionResolver 处理的异常类型有两种:
直接继承自 ResponseStatusException 的异常类,这种异常类可以直接从里边提取出来想要的信息。
通过 @ResponseStatus 注解的普通异常类,这种情况下异常信息从 @ResponseStatus 注解中提取出来。
举一个例子来进行说明:
第一种方式:
public class MyException extends ResponseStatusException {
public MyException(Integer code){
super(HttpStatus.BAD_REQUEST);
}
}
第二种方式:
@ResponseStatus(value= HttpStatus.FORBIDDEN,reason = "用户数量太多")
public class UserTooManyException extends RuntimeException {
public UserTooManyException(){
}
public UserTooManyException(String message){
super(message);
}
}
那么来到最后一个
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver#doResolveException
protected ModelAndView doResolveException(
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
// Expose ModelAndView for chosen error view.
String viewName = determineViewName(ex, request);
if (viewName != null) {
// Apply HTTP status code for error views, if specified.
// Only apply it if we're processing a top-level request.
Integer statusCode = determineStatusCode(request, viewName);
if (statusCode != null) {
applyStatusCodeIfPossible(request, response, statusCode);
}
return getModelAndView(viewName, ex, request);
}
else {
return null;
}
}
没有用过这个,既然排在最后一个,那么这里就不来进行测试使用了。
自定义异常:
@Order(value= Ordered.HIGHEST_PRECEDENCE) //优先级,数字越小优先级越高
@Component
public class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {
// 看看这里传递的参数,那么说明我们也可以这样子来传参
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception ex) {
try {
response.sendError(511,"我喜欢的错误");
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView();
}
}
参考:https://blog.csdn.net/u012702547/article/details/115732704?spm=1001.2014.3001.5501