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

news2024/9/30 13:22:02

本文介绍一下与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集:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/348688.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LeetCode(剑指offer) DAY2

1.题目 从尾到头打印链表 解法一&#xff1a;先统计链表有多少节点&#xff0c;然后创建数组&#xff0c;再次遍历链表将值存储至数组&#xff0c;然后反转数组。这种解法思路简单&#xff0c;但是时间复杂度较高。 class Solution {int a 0,b0;public int[] reversePrint(Li…

Routability-Driven Macro Placement with Embedded CNN-Based Prediction Model

Routability-Driven Macro Placement with Embedded CNN-Based Prediction Model 2019 Design, Automation & Test in Europe Conference & Exhibition (DATE) DOI: 10.23919/DATE.2019.8715126 目录Abstract一、Introduction二、PROBLEM FORMULATION AND PRELIMINARIE…

java分析插入排序

首先查看一张经典的插入排序的图片 有图片可知&#xff0c;插入排序其字面的意思找到小的插入到前面就行 插入排序的基本思想就是分治&#xff1a;将数组分为两个区域&#xff0c;分别是已排序区和没有排序的区域 已排序区&#xff1a;假定一边的数组是都排序好的 wei排序区&…

软件测试选Python还是Java?

目录 前言 1、先从一门语言开始 2、两个语言的区别 3、两个语言的测试栈技术 4、如何选择两种语言&#xff1f; 总结 前言 对于工作多年的从业者来说&#xff0c;同时掌握java和Python两门语言再好不过&#xff0c;可以大大增加找工作时的选择范围。但是对于转行的人或者…

Vue2之Vue-cli应用及组件基础认识

Vue2之Vue-cli应用及组件基础认识一、Vue-cli1、单页面应用程序2、vue-cli介绍3、安装和使用4、创建项目4.1 输入创建项目4.2 选择第三项&#xff0c;进行自主配置&#xff0c;按回车键即可4.3 选择自己需要的库4.4 选择Vue的版本4.5 选择CSS选择器4.6 选择Babel、ESLint、etc等…

Sitara™处理器的产品开发路线图

Sitara™处理器的产品开发路线图概述Evaluation Phase(评估阶段)Board Development Phase(硬件发展阶段&#xff0c;硬件设计人员应重点关注这个阶段)Software Development Phase(软件发展阶段)Product Phase/SW Lifecycle概述 一般情况下&#xff0c;会存在四个主要的发展阶段…

从0到1一步一步玩转openEuler--15 openEuler使用DNF管理软件包

文章目录15.1 搜索软件包15.2 列出软件包清单15.3 显示RPM包信息15.4 安装RPM包15.5 下载软件包15.6 删除软件包DNF是一款Linux软件包管理工具&#xff0c;用于管理RPM软件包。DNF可以查询软件包信息&#xff0c;从指定软件库获取软件包&#xff0c;自动处理依赖关系以安装或卸…

书籍《金字塔原理》读后感

上周读完了书籍《金字塔原理》这本书&#xff0c;这本书在管理学中&#xff0c;比较有名的书籍了&#xff0c;之前没怎么读过跟管理学相关书籍&#xff0c;这本算是第一本&#xff0c;是上级推荐给自己的&#xff0c;自己首先查了下&#xff0c;推荐度还是挺好的&#xff0c;看…

uniapp实现app检查更新与升级-uni-upgrade-center详解

app检查更新与升级 参考链接&#xff1a; 升级中心uni-upgrade-center - App uni-admin h5 api App资源在线升级更新 uni-app使用plus注意事项 关于在线升级&#xff08;WGT&#xff09;的几个疑问 什么是升级中心uni-upgrade-center uniapp官方开发的App版本更新的插件&#…

结构体的不定长数组,用起来就是这么爽

结构体的不定长数组 结构体数组不定长的做法&#xff0c;有两种 第一种&#xff1a; 指针 第二种&#xff1a;长度为0的数组 1. 结构体的数组指针 特点&#xff1a; 结构体初始化时&#xff0c;可以是结构体指针&#xff0c;如struct tag_info *pInfo NULL; 也可以是结构体变量…

Python文件和数据格式化(教程)

文件是一个存储在副主存储器的数据序列&#xff0c;可包含任何数据内容。 概念上&#xff0c;文件是数据的集合和抽象&#xff0c;类似的&#xff0c;函数是程序的集合和抽象。 用文件形式组织和表达数据更有效也更加灵活。 文件包括两种形式&#xff0c;文本文件和二进制文…

推荐几款市面上常用的免费CMS建站系统

小编在网站建设行业从业十几年&#xff0c;很多客户或者朋友找我做网站的时候&#xff0c;都喜欢开发一个完全熟悉自己的网站系统&#xff0c;但是小编这里很不推荐。从0到1全新开发&#xff0c;成本&#xff0c;效率和成熟度这些和主流的cms建站系统比起来&#xff0c;完全没有…

文件与IO

一.文件的定义什么是文件&#xff1f;文件分为狭义上的文件和广义上的文件 狭义上的文件&#xff1a;文件夹中的文件&#xff1a;包括视频、图片、文本、可执行文件等等......其中有些文件是有后缀名的&#xff0c;而有一些文件是没有后缀名的广义上的文件&#xff1a;在Linux系…

Unix Linux、MAC、Window 如何安装配置环境?都在这里啦~

嗨害大家好鸭&#xff01;我是小熊猫~ 这次将向大家介绍如何在本地搭建Python开发环境。 Python可应用于多平台包括 Linux 和 Mac OS X。 你可以通过终端窗口输入 “python” 命令来查看本地是否已经安装Python以及Python的安装版本。 源码资料电子书:点击此处跳转文末名片获…

PHP代码审计神器——RIPS个人汉化版(2017年老文)

一、RIPS简介 RIPS是一款PHP开发的开源的PHP代码审计工具&#xff0c;由国外的安全研究者Johannes Dahse开发&#xff0c;目前开源的最新版本是0.55。 程序小巧玲珑&#xff0c;仅有不到500kb&#xff0c;其中的PHP语法分析非常精准&#xff0c;可以实现跨文件变量和函数追踪…

Virtualbox安装Windows11教程,提供虚机专用镜像下载。

微软在Windows11安装过程中增加了TPM2.0安全验证&#xff0c;所以一些老旧的电脑或者不带TPM认证协议的虚拟机都无法安装系统镜像。 这给我们体验尝鲜带来了一些小小的困扰。其实有2种解决方法可以在虚拟机中体验到Windows11带来的变化。 方法一 虚拟机启动时先加载PE系统镜…

Python 版本的常见算法模板(一)

文章目录前言排序模板排序算法归并排序KMP图邻接表Floyd 算法DijkstraBellMan-Ford 算法SPFA 算法Prim 算法Kruskra 算法染色法Hunger算法前言 翻了翻自己以前写的一些博文&#xff0c;发现的话&#xff0c;还是有一些误区没有写好的&#xff0c;所以的话这里的重新写一下&…

年薪40W的测试工程师被裁,回怼的一番话,令人沉思...

腾讯一位测试工程师被炒&#xff0c;回怼到:“反正我有技术&#xff0c;在哪不一样” 在腾讯上班的朋友给我分享了今天在他公司遇到的事情&#xff0c;他部门一位测试工程师被炒&#xff0c;具体原因好像就是跟上司闹矛盾&#xff0c;部门的都觉得非常可惜&#xff0c;因为他算…

本地修改的文件,使用git stash暂存之后不小心将暂存区清空,重新找回之前被暂存的文件

概述 问题 日常使用git 时&#xff0c;将本地所做的修改使用git stash暂存&#xff0c;使用git pull拉取代码之后&#xff0c;之间用git stash clear将git stash暂存的内容删除掉了。本文讲述如何恢复git stash clear掉的暂存区代码。 解决方法 执行指令 git log --graph -…

银行存取款程序设计(JAVA基础案例教程-黑马程序员编著-第三章-课后作业)

【案例3-2】银行存取款程序设计 【案例介绍】 案例描述 银行存取款的流程是人们非常熟悉的事情&#xff0c;用户可在银行对自己的资金账户进行存款、取款、查询余额等操作&#xff0c;极大的便利了人民群众对资金的管理。 本任务要求&#xff0c;使用所学知识编写一个银行存…