本文介绍一下与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类:
- 管理swagger的Swagger2Controller接口映射
- Swagger2DocumentationConfiguration配置类装配
WebMvcEndpointHandlerMapping
org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping类:
- A custom HandlerMapping that makes web endpoints available over HTTP using Spring MVC
- 管理Spring Boot Actuator中管理监控端点
- WebMvcEndpointManagementContextConfiguration配置类装配
ControllerEndpointHandlerMapping
org.springframework.boot.actuate.endpoint.web.servlet.ControllerEndpointHandlerMapping类:
- HandlerMapping that exposes @ControllerEndpoint and @RestControllerEndpoint annotated endpoints over Spring MVC
- 管理被@ControllerEndpoint或@RestControllerEndpoint标注的Spring MVC端点
RequestMappingHandlerMapping
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping类:
- Creates RequestMappingInfo instances from type and method-level @RequestMapping annotations in @Controller classes
- 管理被@RequestMapping、@Controller请求映射
BeanNameUrlHandlerMapping
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping类:
- 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
- 管理请求url与Bean映射关系
RouterFunctionMapping
org.springframework.web.servlet.function.support.RouterFunctionMapping类:
- 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
- 管理RouterFunction集
SimpleUrlHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping类:
- 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
- 管理请求url与请求处理器Bean映射关系
WelcomePageHandlerMapping
org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping类:
- 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类:
- Extension of AbstractHandlerMethodAdapter that supports @RequestMapping annotated HandlerMethods
- 处理@RequestMapping标注的处理器
HandlerFunctionAdapter
org.springframework.web.servlet.function.support.HandlerFunctionAdapter类:
- HandlerAdapter implementation that supports HandlerFunctions
- 处理HandlerFunction处理器,与RouterFunctionMapping对应
HttpRequestHandlerAdapter
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter类:
- 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
- 处理HttpRequestHandler处理器
SimpleControllerHandlerAdapter
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter类:
- 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
- 处理实现了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 |
ViewResolverComposite | A 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集: