Spring MVC
接口HandlerExceptionResolver
用于抽象一个异常解析器。这种异常解析器被用于分析请求处理器Handler
映射或者执行过程中的异常,将这些异常转换成其他形式展示给用户。典型的做法是转换成一个错误视图,或者返回某个HTTP
状态码给请求端。
源代码版本 : spring-webmvc-5.1.5.RELEASE
package org.springframework.web.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.lang.Nullable;
/**
* Interface to be implemented by objects that can resolve exceptions thrown during
* handler mapping or execution, in the typical case to error views. Implementors are
* typically registered as beans in the application context.
*
* Error views are analogous to JSP error pages but can be used with any kind of
* exception including any checked exception, with potentially fine-grained mappings for
* specific handlers.
*
* @author Juergen Hoeller
* @since 22.11.2003
*/
public interface HandlerExceptionResolver {
/**
* Try to resolve the given exception that got thrown during handler execution,
* returning a ModelAndView that represents a specific error page if appropriate.
* The returned ModelAndView may be 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 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 ModelAndView to forward to,
* or null for default processing in the resolution chain
*/
@Nullable
ModelAndView resolveException(
HttpServletRequest request, // 当前请求对象
HttpServletResponse response, // 当前响应对象
@Nullable Object handler, // 发生异常的handler,也可能为null,因为异常发生时可能不在handler中
Exception ex // 所发生的异常
);
}
Spring MVC
为HandlerExceptionResolver
提供了四个实现类 :
-
ExceptionHandlerExceptionResolver
找到使用
@ExceptionHandler
注解的ServletInvocableHandlerMethod
并调用,基于其执行结果构建ModelAndView
。 -
SimpleMappingExceptionResolver
映射异常类名到视图名称,处理时使用此映射关系构建
ModelAndView
, 或者使用缺省视图。 -
ResponseStatusExceptionResolver
如果异常是
ResponseStatusException
或者使用了注解@ResponseStatus
,则利用这些信息将异常翻译成HTTP
状态字调用响应的sendError
,返回空ModelAndView
。
注意:该异常解析器会递归检查异常的cause
异常。 -
DefaultHandlerExceptionResolver
Spring MVC
缺省异常处理器,解析时将标准MVC
异常翻译成HTTP
状态字调用响应对象的方法#sendError
,返回一个空ModelAndView
对象(注意:不是null
)Exception
HTTP Status Code
HttpRequestMethodNotSupportedException
405 ( SC_METHOD_NOT_ALLOWED
)HttpMediaTypeNotSupportedException
415 ( SC_UNSUPPORTED_MEDIA_TYPE
)HttpMediaTypeNotAcceptableException
406 ( SC_NOT_ACCEPTABLE
)MissingPathVariableException
500 ( SC_INTERNAL_SERVER_ERROR
)MissingServletRequestParameterException
400 ( SC_BAD_REQUEST
)ServletRequestBindingException
400 ( SC_BAD_REQUEST
)ConversionNotSupportedException
500 ( SC_INTERNAL_SERVER_ERROR
)TypeMismatchException
400 ( SC_BAD_REQUEST
)HttpMessageNotReadableException
400 ( SC_BAD_REQUEST
)HttpMessageNotWritableException
500 ( SC_INTERNAL_SERVER_ERROR
)MethodArgumentNotValidException
400 ( SC_BAD_REQUEST
)MissingServletRequestPartException
400 ( SC_BAD_REQUEST
)BindException
400 ( SC_BAD_REQUEST
)NoHandlerFoundException
404 ( SC_NOT_FOUND
)AsyncRequestTimeoutException
503 ( SC_SERVICE_UNAVAILABLE
)
这些接口/类之间的关系如下图所示 :