PathPattern
整理:太虚野老
说明:
- 本文内容来自于Spring Framework 6.0.13。
PathPattern类所在包:org.springframework.web.util.pattern
继承:
java.lang.Object
org.springframework.web.util.pattern.PathPattern
所有实现的接口:
Comparable<PathPattern>
语法:
public class PathPattern extends Object Implements Comparable<PathPattern> |
---|
匹配规则:
解析路径模式的表示形式。包括用于快速匹配的路径元素链和用于快速比较模式的累积计算状态。
PathPattern使用以下规则匹配URL路径:
- ?匹配一个字符
- * 匹配路径段内的零个或多个字符
- ** 匹配零个或多个路径段,直到路径结束
- {spring}匹配路径段并将其捕获为名为“spring”的变量
- {spring:[a-z]+}将正则表达式[a-z]+匹配为名为“spring”的路径变量
- {*spring}匹配零个或多个路径段,直到路径结束,并将其捕获为名为“spring”的变量
注意:与AntPathMatcher相比,**仅在模式末尾才受支持。例如,/pages/{**}有效,但/pages/{**}/details无效。这同样适用于捕获变量{*spring}。其目的是在比较模式的特异性时消除歧义。
示例:
-
/pages/t?st.html —匹配/pages/test.html和/pages/tXst.html,但不匹配/pages/toast.html
-
/resources/*.png—匹配resources目录中的所有.png文件
-
/resources/**—匹配/resources/路径下的所有文件,包括/resources/image.png和/resources/css/spring.css
-
/resources/{*path}—匹配/resources/和/resources下的所有文件,并在名为“path”的变量中捕获它们的相对路径。/resources/image.png将与“path”匹配→ “/image.png”并且/resources/css/spring.css将与“path”匹配→“/css/spring.css”
-
/resources/{filename:\\w+}.dat将匹配/resources/spring.dat,并将值“spring”分配给filename变量
自5.0以后。
嵌套类摘要:
嵌套类 | ||
---|---|---|
修饰符和类型 | 类 | 描述 |
static class | PathPattern.PathMatchInfo | URI变量和基于给定匹配路径的模式提取的路径参数(矩阵变量)的持有者。 |
static class | PathPattern.PathRemainingMatchInfo | 在一个模式开始时,一个匹配结果的持有者。 |
字段摘要:
字段 | ||
---|---|---|
修饰符和类型 | 字段 | 描述 |
static final Comparator<PathPattern> | SPECIFICITY_COMPARATOR | 比较器,按特定性对模式进行排序,如下所示:空实例是最后一个。 |
方法概要:
方法 | ||
---|---|---|
修饰符和类型 | 方法 | 描述 |
PathPattern | combine(PathPattern pattern2string) | 将这种模式与另一种模式合并。 |
int | compareTo(PathPattern otherPattern) | 将此模式与提供的模式进行比较:如果此模式比提供的模式更具体、相同或不太具体,则返回-1,0,+1。 |
boolean | equals(Object other) | |
PathContainer | extractPathWithinPattern(PathContainer path) | 确定给定路径的模式映射部分。 |
String | getPatternString() | 返回为创建此PathPattern而解析的原始字符串。 |
int | hashCode() | |
boolean | hasPatternSyntax() | 模式字符串是否包含需要使用匹配(PathContainer)的模式语法,或者它是一个可以直接与其他字符串进行比较的常规字符串。 |
PathPattern.PathMatchInfo | matchAndExtract(PathContainer pathContainer) | 将此模式与给定的URI路径匹配,并返回提取的URI模板变量以及路径参数(矩阵变量) |
boolean | matches(PathContainer pathContainer) | 此模式是否与给定路径匹配。 |
PathPattern.PathRemainingMatchInfo | matchStartOfPath(PathContainer pathContainer) | 匹配给定路径的开头,并返回此模式未覆盖的剩余部分。 |
String | toString() |
从类java.lang.Object继承的方法:
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
---|
字段详细信息:
SPECIFICITY_COMPARATOR |
---|
public static final Comparator<PathPattern> SPECIFICITY_COMPARATOR
按特定性对模式进行排序的比较器,如下所示:
- 空实例是最后一个。
- 包罗万象的模式排在最后。
- 如果两种模式都是包罗万象的,请考虑长度(越长越好)。
- 比较通配符和捕获的变量计数(较低)。
- 考虑长度(越长越好)
方法详细信息:
getPatternString
public String getPatternString() 返回为创建此PathPattern而解析的原始字符串。 |
---|
hasPatternSyntax
public boolean hasPatternSyntax() 模式字符串是否包含需要使用匹配(PathContainer)的模式语法,或者它是一个可以直接与其他字符串进行比较的常规字符串。 自5.2以后。 |
---|
matches
public boolean matches(PathContainer pathContainer) 此模式是否与给定的路径匹配。 参数: pathContainer——要尝试匹配的候选路径 返回: 如果路径匹配此模式,则为True |
---|
matchAndExtract
@Nullable public PathPattern.PathMatchInfo matchAndExtract(PathContainer pathContainer) 将此模式与给定的URI路径匹配,并返回提取的URI模板变量以及路径参数(矩阵变量)。 参数: pathContainer——要尝试匹配的候选路径 返回: 具有提取变量的info对象,或为null表示不匹配 |
---|
matchStartOfPath
@Nullable public PathPattern.PathRemainingMatchInfo matchStartOfPath(PathContainer pathContainer) 匹配给定路径的开头,并返回未被此模式覆盖的剩余部分。这对于匹配嵌套路由非常有用,其中路径在每个级别上都是增量匹配的。 参数: pathContainer——要尝试匹配的候选路径 返回: 带有匹配结果的info对象,如果不匹配则为空 |
---|
extractPathWithinPattern
public PathContainer extractPathWithinPattern(PathContainer path) 确定给定路径的模式映射部分。 例如: ‘/docs/cvs/commit.html’和’/docs/cvs/commit.html’→’’ ‘/docs/*‘和’/docs/cvs/commit’→’cvs/commit’ ‘/docs/cvs/*.html’和’/docs/cvs/commit.html ‘→’commit.html’ '/docs/**‘和’/docs/cvs/commit ‘→’cvs/commit’ 注: 假设matches(org.springframework.http.server.PathContainer)对相同的路径返回true,但不强制执行此操作。 返回结果中重复出现的分隔符将被删除 从返回结果中删除前导和尾随分隔符 参数: Path -匹配此模式的路径 返回: 由模式匹配的路径的子集,或者“”(如果没有一个由模式元素匹配) |
---|
compareTo
public int compareTo(@Nullable PathPattern otherPattern) 将此模式与提供的模式进行比较:如果此模式比提供的模式更具体、相同或不太具体,则返回-1,0,+1。其目的是首先对更具体的模式进行排序。 规定: comparareto在接口Comparable<PathPattern> |
---|
combine
public PathPattern combine(PathPattern pattern2string) 将这种模式与另一种模式合并。 |
---|
equals
public boolean equals(@Nullable Object other) 重写: equals 在类Object |
---|
hashCode
public int hashCode() 重写: hashCode在类Object |
---|
toString
public String toString() 重写: toString在类Object |
---|
本文PDF文档下载地址:https://download.csdn.net/download/usertest1/88596288