SpringMVC DispatcherServlet源码(4) HandlerMapping和HandlerAdapter等组件说明

29 篇文章 0 订阅
6 篇文章 0 订阅

本文介绍一下与DispacherServlet相关的几个重要组件:

  • HandlerMapping - 管理请求与处理器映射关系
  • HandlerAdapter - 请求处理器
  • HandlerMethodArgumentResolver - 处理器方法参数解析器
  • HandlerMethodReturnValueHandler - 处理器方法返回值处理器
  • HttpMessageConverter - 请求体、响应体读写转换器
  • ViewResolver - 视图解析器
  • HandlerExceptionResolver - 异常处理器

HandlerMapping

Interface to be implemented by objects that define a mapping between requests and handler objects.

管理请求与处理器映射关系。

在Spring Boot中默认会注入8个HandlerMapping:

在这里插入图片描述

PropertySourcedRequestMappingHandlerMapping

springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping类:

  1. 管理swagger的Swagger2Controller接口映射
  2. Swagger2DocumentationConfiguration配置类装配

WebMvcEndpointHandlerMapping

org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping类:

  1. A custom HandlerMapping that makes web endpoints available over HTTP using Spring MVC
  2. 管理Spring Boot Actuator中管理监控端点
  3. WebMvcEndpointManagementContextConfiguration配置类装配

ControllerEndpointHandlerMapping

org.springframework.boot.actuate.endpoint.web.servlet.ControllerEndpointHandlerMapping类:

  1. HandlerMapping that exposes @ControllerEndpoint and @RestControllerEndpoint annotated endpoints over Spring MVC
  2. 管理被@ControllerEndpoint或@RestControllerEndpoint标注的Spring MVC端点

RequestMappingHandlerMapping

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping类:

  1. Creates RequestMappingInfo instances from type and method-level @RequestMapping annotations in @Controller classes
  2. 管理被@RequestMapping、@Controller请求映射

BeanNameUrlHandlerMapping

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping类:

  1. Implementation of the HandlerMapping interface that maps from URLs to beans with names that start with a slash (“/”), similar to how Struts maps URLs to action names
  2. 管理请求url与Bean映射关系

RouterFunctionMapping

org.springframework.web.servlet.function.support.RouterFunctionMapping类:

  1. HandlerMapping implementation that supports RouterFunctions. If no RouterFunction is provided at construction time, this mapping will detect all router functions in the application context, and consult them in order
  2. 管理RouterFunction集

SimpleUrlHandlerMapping

org.springframework.web.servlet.handler.SimpleUrlHandlerMapping类:

  1. Implementation of the HandlerMapping interface that maps from URLs to request handler beans. Supports both mapping to bean instances and mapping to bean names; the latter is required for non-singleton handlers
  2. 管理请求url与请求处理器Bean映射关系

WelcomePageHandlerMapping

org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping类:

  1. An AbstractUrlHandlerMapping for an application’s welcome page. Supports both static and templated files. If both a static and templated index page are available, the static page is preferred

HandlerAdapter

请求处理器。

在这里插入图片描述

RequestMappingHandlerAdapter

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter类:

  1. Extension of AbstractHandlerMethodAdapter that supports @RequestMapping annotated HandlerMethods
  2. 处理@RequestMapping标注的处理器

HandlerFunctionAdapter

org.springframework.web.servlet.function.support.HandlerFunctionAdapter类:

  1. HandlerAdapter implementation that supports HandlerFunctions
  2. 处理HandlerFunction处理器,与RouterFunctionMapping对应

HttpRequestHandlerAdapter

org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter类:

  1. Adapter to use the plain HttpRequestHandler interface with the generic DispatcherServlet. Supports handlers that implement the LastModified interface. This is an SPI class, not used directly by application code
  2. 处理HttpRequestHandler处理器

SimpleControllerHandlerAdapter

org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter类:

  1. Adapter to use the plain Controller workflow interface with the generic DispatcherServlet. Supports handlers that implement the LastModified interface. This is an SPI class, not used directly by application code
  2. 处理实现了Controller接口的处理器

HandlerMethodArgumentResolver参数解析器

HandlerMethodArgumentResolver接口

Strategy interface for resolving method parameters into argument values in the context of a given request.

处理器方法参数解析器。

在这里插入图片描述

public interface HandlerMethodArgumentResolver {

	/**
	 * 判断当前解析器实现类是否支持目标参数
	 */
	boolean supportsParameter(MethodParameter parameter);

	/**
	 * Resolves a method parameter into an argument value from a given request
	 */
	Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception;
}

HandlerMethodArgumentResolver实现类

实现类作用
RequestParamMethodArgumentResolver解析@RequestParam注解标注的参数,MultipartFile类型参数也可以解析
支持被@RequestParam注解标注的、且指定了注解name值的Map类型参数
如果参数类型是简单类型,即使不被@RequestParam注解标注,该解析器也支持
RequestParamMapMethodArgumentResolver解析参数类型为Map、被@RequestParam注解标注、且注解没有指定name的参数
PathVariableMethodArgumentResolver解析被@PathVariable标注的参数
支持Map类型、被@PathVariable标注、且指定注解name值的参数
PathVariableMapMethodArgumentResolver解析被@PathVariable标注、Map类型、且注解未指定name的参数
RequestResponseBodyMethodProcessor解析被@RequestBody注解标注的参数
RequestHeaderMethodArgumentResolver解析被@RequestHeader标注的参数
RequestHeaderMapMethodArgumentResolver解析被@RequestHeader标注、且为Map类型的参数
ServletCookieValueMethodArgumentResolver解析被@CookieValue标注的参数
ExpressionValueMethodArgumentResolver解析被@Value标注的参数
SessionAttributeMethodArgumentResolver解析被@SessionAttribute标注的参数
RequestAttributeMethodArgumentResolver解析被@RequestAttribute标注的参数
ServletRequestMethodArgumentResolver解析servlet请求内置参数ServletRequest、MultipartRequest、HttpSession等
ServletResponseMethodArgumentResolver解析servlet响应内置参数ServletResponse、OutputStream等
HttpEntityMethodProcessor解析RequestEntity类型参数
MapMethodProcessor解析Map类型参数

HandlerMethodReturnValueHandler返回值处理器

HandlerMethodReturnValueHandler接口

Strategy interface to handle the value returned from the invocation of a handler method.

处理器方法返回值处理器。

在这里插入图片描述

public interface HandlerMethodReturnValueHandler {

	/**
	 * 判断当前处理器是否支持返回值类型
	 */
	boolean supportsReturnType(MethodParameter returnType);

	/**
	 * 处理返回值
	 */
	void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
			ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;
}

HandlerMethodReturnValueHandler实现类

实现类作用
ModelAndViewMethodReturnValueHandler处理ModelAndView类型返回值,
如果返回值为null且ModelAndViewContainer.setRequestHandled(true)
该请求已经在处理器里面处理完成
ViewMethodReturnValueHandler处理View类型返回值
HttpEntityMethodProcessor处理ResponseEntity类型返回值
HttpHeadersReturnValueHandler处理HttpHeaders类型返回值
CallableMethodReturnValueHandler处理Callable类型返回值
DeferredResultMethodReturnValueHandler处理DeferredResult类型返回值
AsyncTaskMethodReturnValueHandler处理WebAsyncTask类型返回值
ModelAttributeMethodProcessor处理ModelAttribute类型返回值
RequestResponseBodyMethodProcessor处理@ResponseBody标注的方法的返回值
ViewNameMethodReturnValueHandler视图名返回值处理器
MapMethodProcessor处理Map类型返回值

HttpMessageConverter数据转换器

HttpMessageConverter接口

Strategy interface for converting from and to HTTP requests and responses.

public interface HttpMessageConverter<T> {

	/**
	 * 判断可以反序列化目标类型
	 */
	boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);

	/**
	 * 判断可以序列化目标类型
	 */
	boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);

	/**
	 * 返回支持的MediaType
	 */
	List<MediaType> getSupportedMediaTypes();

	/**
	 * 反序列化
	 */
	T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
			throws IOException, HttpMessageNotReadableException;

	/**
	 * 序列化
	 */
	void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException;
}

HttpMessageConverter实现类

在这里插入图片描述

实现类作用
ByteArrayHttpMessageConverter支持byte[]的读写
StringHttpMessageConverter支持String的读写
ResourceHttpMessageConverter支持Resource的读写
MappingJackson2HttpMessageConverter使用jackson读写json数据
Jaxb2RootElementHttpMessageConverter支持xml jaxb2读写

ViewResolver视图解析器

ViewResolver接口

通过视图名获取视图对象。

public interface ViewResolver {

	/**
	 * Resolve the given view by name
	 */
	View resolveViewName(String viewName, Locale locale) throws Exception;
}

ViewResolver实现类

在这里插入图片描述

实现类作用
ContentNegotiatingViewResolver
BeanNameViewResolver从spring容器获取view
ViewResolverCompositeA ViewResolver that delegates to others
InternalResourceViewResolver内部资源解析器,比如redirect:和forward:等

HandlerExceptionResolver异常处理器

处理异常。

HandlerExceptionResolver接口:

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 empty to indicate that the exception has been 
     * resolved successfully but that no view should be rendered, for instance by setting a status code.
	 */
	ModelAndView resolveException(
			HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);
}

HandlerExceptionResolver集:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值