当前位置: 首页 > news >正文

【老王读SpringMVC-3】根据 url 是如何找到 controller method 的?

前面分析了 request 与 handler method 映射关系的注册,现在再来分析一下 SpringMVC 是如何根据 request 来获取对应的 handler method 的?

可能有人会说,既然已经将 request 与 handler method 映射关系注册保存在了 AbstractHandlerMethodMapping.MappingRegistry#registry 中,那么根据 request 不就能直接从 registry 中获取到相应的 handler method 了吗?

如果我们定义的 Controller 中的 @RequestMapping 都是普通的字符的话,那确实是可以直接通过 registry 获取 handler method。
但是,@RequestMapping 还支持占位符、通配符等 url,例如:

@RequestMapping("/resources/ima?e.png") // 匹配路径段中的一个字符
@RequestMapping("/resources/*.png") // 匹配路径段中的零个或多个字符
@RequestMapping("/resources/**") // 匹配多个路径段
@RequestMapping("/projects/{project}/versions") // 匹配路径段并将其捕获为变量
@RequestMapping("/projects/{project:[a-z]+}/versions") // 使用正则表达式匹配并捕获变量

所以,查找 request 对应的 handler method 就不是那么简单的事情了。

如何通过 request 获取 handler?

通过对 DispatcherServlet 的分析,我们知道 SpringMVC 获取 handler 的方法如下:
org.springframework.web.servlet.DispatcherServlet#getHandler()

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {if (this.handlerMappings != null) {for (HandlerMapping mapping : this.handlerMappings) {HandlerExecutionChain handler = mapping.getHandler(request);if (handler != null) {return handler;}}}return null;
}

可以看出,SpringMVC 是通过 HandlerMapping#getHandler() 来获取 request 对应的 handler method 处理程序的,最终会拿到一个 HandlerExecutionChain

HandlerExecutionChain 中包含了 request 对应的 handler method 和 interceptors。

HandlerMapping 的类图如下:
HandlerMapping.png

HandlerMapping 的实现类中其中最常用的是 RequestMappingHandlerMapping,它是专门用来处理 @RequestMapping 定义的 request 请求映射的。

获取 HandlerExecutionChain 的过程

HandlerExecutionChain 的获取是在 AbstractHandlerMapping#getHandler() 中完成的。
主要分成了两步:
1、获取 request 对应的 handler 程序
2、将 handler 和 相应的 HandlerInterceptors 组装成 HandlerExecutionChain

getHandler.png

获取 request 对应的 handler method 的过程

上一步获取 HandlerExecutionChain 时,会调用 AbstractHandlerMethodMapping#getHandlerInternal() 来获取 request 对应的 handler method。
最终它会调用到 AbstractHandlerMethodMapping#lookupHandlerMethod() 来获取。

lookupHandlerMethod.png

可以看到,AbstractHandlerMethodMapping#lookupHandlerMethod 实现了 request 与 handler method 映射关系的查找:
1、首先,通过 directPath 直接获取映射的 handler method
2、如果通过 directPath 没有找到的话,就循环 registry 中所有的映射,查出映射关系
3、如果获取到的映射的 handler 个数 > 1 的话,就找出最合适的匹配

DirectPath: 非 patterns 的 path。
非 patterns 的 path 是指 path 的定义中没有 ?、*、{} 的 path

通过 lookupPath 获取 HandlerMethod

AbstractHandlerMethodMapping#lookupHandlerMethod() 在寻找 HandlerMethod 时,有两种逻辑:

  • 1、直接通过 directPath 快速查找
  • 2、循环所有的映射关系,查找 RequestCondition 条件匹配,获取最合适的匹配

直接通过 directPath 快速查找

directPath 是最快速的方式,直接从 map 中获取匹配结果。
directPath 是不包含占位符、模式匹配符的普通路径,所以这种普通路径是最直接,也是效率最高的。

循环所有的映射关系,通过 RequestCondition 条件匹配,获取最合适的匹配

如果通过 directPath 没有找到任何匹配的话,就需要遍历所有注册的映射关系,找出最合适的匹配。


addMatchingMappings(this.mappingRegistry.getRegistrations().keySet(), matches, request);// 这里会循环所有注册的映射关系,将满足 RequestCondition 的映射查找出来,存放到 matches 中
private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {for (T mapping : mappings) {T match = getMatchingMapping(mapping, request);if (match != null) {matches.add(new Match(match, this.mappingRegistry.getRegistrations().get(mapping)));}}
}

对于 @RequestMapping 注解形式的 url 映射,AbstractHandlerMethodMapping#getMatchingMapping() 最终会调用实现类的方法 RequestMappingInfoHandlerMapping#getMatchingCondition() 来获取匹配。

getMatchingCondition.png

可以看到,Spring 会检查 RequestMappingInfo 是否满足以下 8 个条件:
1、请求方法 RequestMethod 是否匹配,如:GET, POST, PUT, DELETE 等
2、请求参数 params 是否匹配
3、请求头 header 是否匹配
4、consumes Content-Type 是否匹配
5、produces Content-Type 是否匹配
6、处理 directPath 和 通配符的请求映射匹配
7、处理 ant 风格的请求映射匹配
8、处理自定义的请求映射条件匹配

关于 RequestCondition 的详细分析查看 @RequstMapping和RequestCondition

小结

SpringMVC 是通过 HandlerMapping#getHandler() 来获取 request 对应的 handler method 处理程序的。
底层实现是通过 AbstractHandlerMethodMapping#lookupHandlerMethod 来查找 request 对应的 handler method:
1、首先,通过 directPath 直接获取映射的 handler method
2、如果通过 directPath 没有找到的话,就循环 registry 中所有的映射,查出映射关系
3、如果获取到的映射的 handler 个数 > 1 的话,就找出最合适的匹配

对于 url 映射中的通配符、ant 风格的请求,都是通过 RequestCondition 接口来进行处理的。

http://www.lryc.cn/news/62918.html

相关文章:

  • 人机交互到艺术设计及玫瑰花绘制实例
  • 多臂老虎机问题
  • DNS 查询原理详解
  • 浅谈软件测试工程师的技能树
  • 转型产业互联网,新氧能否再造辉煌?
  • CRE66365 应用资料
  • vue3快速上手学习笔记,还不快来看看?
  • HDU 5927 Auxiliary Set
  • 24:若所有参数皆需类型转换,请为此采用non-member函数
  • CMake(2)-详解-编译-安装-支持GDB-添加环境检查-添加版本号-生成安装包
  • java面试题(redis)
  • Vue组件懒加载
  • Qt音视频开发42-网络推流(视频推流/本地摄像头推流/桌面推流/网络摄像头转发推流等)
  • 更简单的存取Bean方式-@Bean方法注解
  • 边缘计算与AI布署应用电力物联网解决方案-RK3588开发平台
  • centos部署unity accelerator
  • HANA开发指南
  • 请问你见过吐代码的泡泡吗(冒泡排序)
  • 【VM服务管家】VM4.0平台SDK_2.1环境配置类
  • 最新研究:可审计的具有拜占庭鲁棒的联邦学习方案
  • JDK1.8下载、安装和环境配置教程
  • 天津超算,青索帮助文档
  • SpringMVC的拦截器和异常处理器
  • 查看库文件是32位还是64位|查看lib是静态库还是导入库|判断是debug模式还是release模式
  • Python小姿势 - Python爬取数据的库——Scrapy
  • [C++初阶]栈和队列_优先级队列的模拟实现 deque类 的理解
  • Spring是什么?关于Spring家族
  • 自然语言处理数据集集锦(持续更新ing...)
  • 93、Dehazing-NeRF: Neural Radiance Fields from Hazy Images
  • JAVA子类与继承