Spring 概念模型 : PathMatcher 路径匹配器
源代码版本 : spring-webmvc-5.1.4.RELEASE
概述
PathMatcher
是Spring
的一个概念模型接口,该接口抽象建模了概念"路径匹配器",一个"路径匹配器"是一个用于路径匹配的工具。它的使用者是 :
org.springframework.core.io.support.PathMatchingResourcePatternResolver
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
org.springframework.web.servlet.mvc.WebContentInterceptor
Spring
框架自身对概念模型接口也提供了一个缺省的实现AntPathMatcher
,用于匹配Ant
风格的路径。
PathMatcher
接口源代码
PathMatcher
接口源代码如下 :
package org.springframework.util;import java.util.Comparator;
import java.util.Map;public interface PathMatcher {/*** Does the given path represent a pattern that can be matched* by an implementation of this interface?* 判断指定的路径 path 是否是一个 pattern(模式)* 如果返回值是 false,也就是说 path 不是一个模式,而是一个静态路径(真正的路径字符串),* 那么就不用调用方法 #match 了,因为对于静态路径的匹配,直接使用字符串等号比较就足够了。* @param path the path String to check* @return true if the given path represents a pattern*/boolean isPattern(String path);/*** Match the given path against the given pattern,* according to this PathMatcher's matching strategy.* 根据当前 PathMatcher 的匹配策略,检查指定的径 path 和指定的模式 pattern 是否匹配* @param 用于检测路径字符串是否匹配于某个模式时所用的模式* @param path 需要被检测的路径字符串* @return true 表示匹配, false 表示不匹配*/boolean match(String pattern, String path);/*** Match the given path against the corresponding part of the given* pattern, according to this PathMatcher's matching strategy.* 根据当前 PathMatcher 的匹配策略,检查指定的径 path 和指定的模式 pattern 是否之间* 是否为前缀匹配* @param pattern the pattern to match against* @param path the path String to test* @return true 表示匹配, false 表示不匹配*/boolean matchStart(String pattern, String path);/*** Given a pattern and a full path, determine the pattern-mapped part.* 给定一个模式 pattern 和一个全路径 path,判断路径中和模式匹配的部分。** This method is supposed to find out which part of the path is matched* dynamically through an actual pattern, that is, it strips off a statically* defined leading path from the given full path, returning only the actually* pattern-matched part of the path.* 该方法用于发现路径中的哪一部分是和模式能动态匹配上的部分。它会去除路径中开头静态部分,* 仅仅返回那部分真正和模式匹配的上的部分。* 例子 : "myroot/*.html" 为 pattern , "myroot/myfile.html" 为路径,* 则该方法返回 "myfile.html". * 具体的检测规则根据当前 PathMatcher 的匹配策略来顶。 * A simple implementation may return the given full path as-is in case* of an actual pattern, and the empty String in case of the pattern not* containing any dynamic parts (i.e. the pattern parameter being* a static path that wouldn't qualify as an actual #isPattern pattern.* A sophisticated implementation will differentiate between the static parts* and the dynamic parts of the given path pattern.* @param pattern the path pattern* @param path the full path to introspect* @return the pattern-mapped part of the given path* (never null)*/String extractPathWithinPattern(String pattern, String path);/*** Given a pattern and a full path, extract the URI template variables. URI template* variables are expressed through curly brackets ('{' and '}').* 给定一个模式和一个路径,提取其中的 URI 模板变量信息。URI模板变量表达式格式为 "{variable}"* * 例子 : pattern 为 "/hotels/{hotel}" ,路径为 "/hotels/1", 则该方法会返回一个 map ,* 内容为 : "hotel"->"1".* @param pattern the path pattern, possibly containing URI templates* @param path the full path to extract template variables from* @return a map, containing variable names as keys; variables values as values*/Map<String, String> extractUriTemplateVariables(String pattern, String path);/*** Given a full path, returns a Comparator suitable for sorting patterns* in order of explicitness for that path.* The full algorithm used depends on the underlying implementation,* but generally, the returned Comparator will sort a list so that more * specific patterns come before generic patterns.* @param path the full path to use for comparison* @return a comparator capable of sorting patterns in order of explicitness*/Comparator<String> getPatternComparator(String path);/*** Combines two patterns into a new pattern that is returned.* The full algorithm used for combining the two pattern depends on the underlying implementation.* 合并两个模式。具体合并的算法由实现类决定。* @param pattern1 the first pattern* @param pattern2 the second pattern* @return the combination of the two patterns* @throws IllegalArgumentException when the two patterns cannot be combined*/String combine(String pattern1, String pattern2);}
从接口代码来理解概念还是有些抽象,下面我们列举一些基于实现类AntPathMatcher
的例子来增强理解 。
AntPathMatcher
使用例子
AntPathMatcher antPathMatcher = new AntPathMatcher();antPathMatcher.isPattern("/user/001");// 返回 false
antPathMatcher.isPattern("/user/*"); // 返回 true
antPathMatcher.match("/user/001","/user/001");// 返回 true
antPathMatcher.match("/user/*","/user/001");// 返回 true
antPathMatcher.matchStart("/user/*","/user/001"); // 返回 true
antPathMatcher.matchStart("/user/*","/user"); // 返回 true
antPathMatcher.matchStart("/user/*","/user001"); // 返回 false
antPathMatcher.extractPathWithinPattern("uc/profile*","uc/profile.html"); // 返回 profile.html
antPathMatcher.combine("uc/*.html","uc/profile.html"); // uc/profile.html
Spring 概念模型 : PathMatcher 路径匹配器相关推荐
- PathMatcher 路径匹配器
1. 参考链接:https://blog.csdn.net/andy_zhang2007/article/details/88884286 package org.springframework.ut ...
- Spring路径匹配器AntPathMatcher
文章目录 PathMatcher接口 使用场景 接口方法 AntPathMatcher类 匹配规则 主要方法 1. isPattern 2. match 3. matchStart 4. extrac ...
- AntPathMatcher路径匹配器
有志者,事竟成 文章持续更新,可以关注[小奇JAVA面试]第一时间阅读,回复[资料]获取福利,回复[项目]获取项目源码,回复[简历模板]获取简历模板,回复[学习路线图]获取学习路线图. 文章目录 一. ...
- 过滤器匹配符包含单词_Hamcrest包含匹配器
过滤器匹配符包含单词 与Hamcrest 1.2相比 ,针对Matchers类的Hamcrest 1.3 Javadoc文档为该类的几种方法添加了更多文档. 例如,四个重载的contains方法具有更 ...
- 【图像配准】多图配准/不同特征提取算法/匹配器比较测试
前言 本文首先完成之前专栏前置博文未完成的多图配准拼接任务,其次对不同特征提取器/匹配器效率进行进一步实验探究. 各类算法原理简述 看到有博文[1]指出,在速度方面SIFT<SURF<BR ...
- spaCy V3.0 基于规则匹配(2)----高效的短语匹配器和依存句法匹配器
1 短语匹配器(PhraseMatcher) 1.1 基本用法 对于需要匹配大型术语列表的情况,可以通过PhraseMatcher和创建Doc对象来代替词符匹配模式(token patterns),可 ...
- Jest 测试框架 expect 和 匹配器 matcher 的设计原理解析
副标题:SAP Spartacus SSR 优化的单元测试分析之二 - 调用参数检测 源代码: it(`should pass parameters to the original engine in ...
- Mockito匹配器优先
这篇文章是意见. 让我们看一下Mockito中用于在Java中进行测试的verify方法. 示例: verify(myMock).someFunction(123) –期望在模拟ONCE上使用输入12 ...
- Hamcrest匹配器常用方法总结
一.Hamcrest是什么? Hamcrest is a library of matchers, which can be combined in to create flexible expres ...
- SLAM Cartographer(13)基于Ceres库的扫描匹配器
SLAM Cartographer(13)基于Ceres库的扫描匹配器 1. 扫描匹配器 2. 残差计算 2.1. 平移残差 2.2. 角度残差 2.3. 占据空间残差 1. 扫描匹配器 通过< ...
最新文章
- 2017年全球光伏需求有望首次突破100吉瓦
- Python中包含义及其定义
- springboot整合redis集群master宕机后连接超时
- 48. C# -- 事件
- 深圳科技园血案:程序员砍杀产品经理
- Oracle笔记 三、function 、select
- Orleans的集群构建
- oracle取消备份存放本地,Oracle自动备份,压缩打包,删除原文件
- IPC--进程间通信五(信号)
- 163邮箱苹果设置不成功_苹果变安卓不是不可能,Corellium让iPhone成功安装安卓系统...
- JavaSE 学习参考:枚举类型
- python常见运维脚本_Python运维常用脚本
- 继承ActionSupport例子展示
- html仿excel冻结 css,css实例:实现gridview仿excel冻结列
- 用Hydra工具暴力破解Windows7管理员密码并访问它的共享服务
- 笨方法学python3怎么样_笨办法学Python3
- 普通话测试第四题评分标准_普通话等级考试《评分细则》.docx
- java中去字符串中的(全角)空格
- linux安装VNC远程桌面环境
- Oracle Data Guard 的角色转换