Spring MVC 概念模型 : 接口 HandlerExceptionResolver

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 MVCHandlerExceptionResolver提供了四个实现类 :

  1. ExceptionHandlerExceptionResolver

    找到使用@ExceptionHandler注解的ServletInvocableHandlerMethod并调用,基于其执行结果构建ModelAndView

  2. SimpleMappingExceptionResolver

    映射异常类名到视图名称,处理时使用此映射关系构建ModelAndView, 或者使用缺省视图。

  3. ResponseStatusExceptionResolver

    如果异常是ResponseStatusException或者使用了注解@ResponseStatus,则利用这些信息将异常翻译成HTTP状态字调用响应的sendError,返回空ModelAndView
    注意:该异常解析器会递归检查异常的cause异常。

  4. DefaultHandlerExceptionResolver

    Spring MVC缺省异常处理器,解析时将标准MVC异常翻译成HTTP状态字调用响应对象的方法#sendError,返回一个空ModelAndView对象(注意:不是null)

    ExceptionHTTP Status Code
    HttpRequestMethodNotSupportedException405 (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)

这些接口/类之间的关系如下图所示 :
HandlerExceptionResolver

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值