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异常。 -
DefaultHandlerExceptionResolverSpring MVC缺省异常处理器,解析时将标准MVC异常翻译成HTTP状态字调用响应对象的方法#sendError,返回一个空ModelAndView对象(注意:不是null)ExceptionHTTP Status CodeHttpRequestMethodNotSupportedException405 ( SC_METHOD_NOT_ALLOWED)HttpMediaTypeNotSupportedException415 ( SC_UNSUPPORTED_MEDIA_TYPE)HttpMediaTypeNotAcceptableException406 ( SC_NOT_ACCEPTABLE)MissingPathVariableException500 ( SC_INTERNAL_SERVER_ERROR)MissingServletRequestParameterException400 ( SC_BAD_REQUEST)ServletRequestBindingException400 ( SC_BAD_REQUEST)ConversionNotSupportedException500 ( SC_INTERNAL_SERVER_ERROR)TypeMismatchException400 ( SC_BAD_REQUEST)HttpMessageNotReadableException400 ( SC_BAD_REQUEST)HttpMessageNotWritableException500 ( SC_INTERNAL_SERVER_ERROR)MethodArgumentNotValidException400 ( SC_BAD_REQUEST)MissingServletRequestPartException400 ( SC_BAD_REQUEST)BindException400 ( SC_BAD_REQUEST)NoHandlerFoundException404 ( SC_NOT_FOUND)AsyncRequestTimeoutException503 ( SC_SERVICE_UNAVAILABLE)
这些接口/类之间的关系如下图所示 :

本文深入探讨SpringMVC中的HandlerExceptionResolver接口及其四种实现类,包括ExceptionHandlerExceptionResolver、SimpleMappingExceptionResolver、ResponseStatusExceptionResolver和DefaultHandlerExceptionResolver。详细解释了它们如何处理和转换异常,以提供用户友好的错误视图或HTTP状态码。
5211

被折叠的 条评论
为什么被折叠?



