Spring MVC 概念模型 -- 接口 HandlerExceptionResolver

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/andy_zhang2007/article/details/88594280

Spring MVC接口HandlerExceptionResolver用于抽象一个异常解析器。这种异常解析器被用于分析请求处理器handler映射或者执行过程中的异常,将这些异常转换成其他形式展示给用户。典型的做法是转换成一个错误视图,或者返回某个HTTP状态码给请求端。

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注解的逻辑处理异常

  2. ResponseStatusExceptionResolver – 使用@ResponseStatus注解信息将异常翻译成HTTP状态字

  3. DefaultHandlerExceptionResolverSpring MVC缺省异常处理器,将标准MVC异常翻译成HTTP状态字

    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)

这些接口/类之间的关系如下图所示 :
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/andy_zhang2007/article/details/88594280