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

springMVC是如何做url映射到controller的?

背景

最近要做一个URL到权限的控制,需要根据实际的URL匹配到配置文件,一般的url好处理,但是对于path parameter 的URL就需要做匹配了,所以研读了一下springmvc的源码,看看是如何做的

  • 入口 FrameworkServlet
    • .doPost(HttpServletRequest request, HttpServletResponse response)
  • DispatcherServlet
    • .doService
    • DispatcherServlet.doDispatch
    • HandlerExecutionChain mappedHandler =getHandler(processedRequest)
      这里有多个handlerMapping,实际用到的是RequestMappingHandlerMapping
this.handlerMappings ={ArrayList@14007} size =7
0={WebMvcEndpointHandlerMapping@13963}
1={ControllerEndpointHandlerMapping@14199}
2={RequestMappingHandlerMapping@9698}
3={BeanNameUrlHandlerMapping@14200}
4= {RouterFunctionMapping@14201}
5={SimpleUrlHandlerMapping@14202}
6={WelcomePageHandlerMapping@14203}
  • AbstractHandlerMethodMapping
    从这个mapping 中获取RequestMappingHandlerMapping
    • HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request)
	protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {List<Match> matches = new ArrayList<>();List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);if (directPathMatches != null) {addMatchingMappings(directPathMatches, matches, request);}if (matches.isEmpty()) {// No choice but to go through all mappings...addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, request);}

这段代码有意思,先去根据url 去map中找RequestMappingInfo (封装了URL匹配相关信息) ,然后去做URL匹配

    • MappingRegistry 保存了所有的配置
mappingLookup 保存了RequestMappingInfo 和 HandlerMethod 的映射
MultiValueMap<String, T> urlLookup 保存了路径和方法的映射
  • 所有的配置信息封装在 RequestMappingInfo
	public RequestMappingInfo getMatchingCondition(HttpServletRequest request) {RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request);if (methods == null) {return null;}ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request);if (params == null) {return null;}HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request);if (headers == null) {return null;}ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request);if (consumes == null) {return null;}ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request);if (produces == null) {return null;}PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(request);if (patterns == null) {return null;}RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request);if (custom == null) {return null;}return new RequestMappingInfo(this.name, patterns,methods, params, headers, consumes, produces, custom.getCondition());}

找到PatternsRequestCondition ,用来做和url做模式匹配

  • PatternsRequestCondition
this.pathMatcher = pathMatcher != null ? pathMatcher : new AntPathMatcher();this.pathMatcher.match("/Task/GetTaskById/{taskid}", "/Task/GetTaskById/123")

这里实际上是spring 的AntPathMatcher 类,直接调用即可匹配带参数的URL

总结

springMVC 做URL匹配,实际上分两步,1,用url去map中找List directPathMatches
2, 遍历List directPathMatches,逐个做pathMatcher

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • HTML(六)——HTML表单和框架
  • 数据结构从入门到精通二 ~ 数组和链表
  • 一线大厂java面试题
  • go语言Gin框架的学习路线(九)
  • 构造+位运算,CF 1901C - Add, Divide and Floor
  • mac M1安装换脸Roop教程及所遇到的问题
  • 微信小程序:多图片显示及图片点击放大,多视频显示
  • git的一些使用技巧(git fetch 和 git pull的区别,git merge 和 git rebase的区别)
  • milvus的批量向量搜索
  • 数模·插值和拟合算法
  • 【Zotero插件】Zotero Tag为文献设置阅读状态 win11下相关设置
  • 上海市计算机学会竞赛平台2022年9月月赛丙组二叉树的遍历
  • 【JavaScript】 JS 的单线程和浏览器的多进程架构
  • PHP常量
  • 图灵测试:人工智能与人类沟通的界限
  • 〔开发系列〕一次关于小程序开发的深度总结
  • GraphQL学习过程应该是这样的
  • IDEA 插件开发入门教程
  • JavaScript设计模式与开发实践系列之策略模式
  • js对象的深浅拷贝
  • redis学习笔记(三):列表、集合、有序集合
  • tweak 支持第三方库
  • vagrant 添加本地 box 安装 laravel homestead
  • 初识 beanstalkd
  • 分享一份非常强势的Android面试题
  • 好的网址,关于.net 4.0 ,vs 2010
  • 老板让我十分钟上手nx-admin
  • 【干货分享】dos命令大全
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • Semaphore
  • 好程序员web前端教程分享CSS不同元素margin的计算 ...
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • #pragma once
  • (04)odoo视图操作
  • (2024.6.23)最新版MAVEN的安装和配置教程(超详细)
  • (4)(4.6) Triducer
  • (AngularJS)Angular 控制器之间通信初探
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (k8s中)docker netty OOM问题记录
  • (poj1.3.2)1791(构造法模拟)
  • (Ruby)Ubuntu12.04安装Rails环境
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (六)Flink 窗口计算
  • (算法)硬币问题
  • (五十)第 7 章 图(有向图的十字链表存储)
  • (游戏设计草稿) 《外卖员模拟器》 (3D 科幻 角色扮演 开放世界 AI VR)
  • (转载)Linux网络编程入门
  • (转载)PyTorch代码规范最佳实践和样式指南
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .chm格式文件如何阅读
  • .net Application的目录