Class AntPathMatcher
匹配URL的规则如下:
?匹配单个字符*匹配0个或多个字符**0个或多个路径中的目录节点{bus:[a-z]+}匹配以"bus"命名的正则[a-z]+
示例:
com/t?st.jsp— 匹配com/test.jsp或com/tast.jsp或com/txst.jspcom/*.jsp— 匹配com目录下全部.jsp文件com/**/test.jsp— 匹配com目录下全部test.jsp文件org/bus/**/*.jsp— 匹配org/bus路径下全部.jsp文件org/**/servlet/bla.jsp— 匹配org/bus/servlet/bla.jsp或org/bus/testing/servlet/bla.jsp或org/servlet/bla.jspcom/{filename:\\w+}.jsp匹配com/test.jsp并将test关联到filename变量
注意: 表达式和路径必须都为绝对路径或都为相对路径。
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classTests whether a string matches against a pattern via aPattern.protected static classThe defaultComparatorimplementation returned bygetPatternComparator(String). -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescription使用DEFAULT_PATH_SEPARATOR作为分隔符构造AntPathMatcher(String pathSeparator) 使用自定义的分隔符构造 -
Method Summary
Modifier and TypeMethodDescriptionCombine two patterns into a new pattern.protected boolean执行匹配,判断给定的path是否匹配patternextractPathWithinPattern(String pattern, String path) Given a pattern and a full path, determine the pattern-mapped part.extractUriTemplateVariables(String pattern, String path) 提取参数getPatternComparator(String path) Given a full path, returns aComparatorsuitable for sorting patterns in order of explicitness.protected AntPathMatcher.AntPathStringMatchergetStringMatcher(String pattern) Build or retrieve anAntPathMatcher.AntPathStringMatcherfor the given pattern.boolean判断给定路径是否是表达式boolean给定路径是否匹配表达式booleanmatchStart(String pattern, String path) 前置部分匹配setCachePatterns(boolean cachePatterns) Specify whether to cache parsed pattern metadata for patterns passed into this matcher'smatch(java.lang.String, java.lang.String)method.setCaseSensitive(boolean caseSensitive) 设置是否大小写敏感,默认为truesetPathSeparator(String pathSeparator) 设置路径分隔符setTrimTokens(boolean trimTokens) 设置是否去除路径节点两边的空白符,默认为falseprotected String[]tokenizePath(String path) Tokenize the given path into parts, based on this matcher's settings.protected String[]tokenizePattern(String pattern) Tokenize the given path pattern into parts, based on this matcher's settings.
-
Field Details
-
DEFAULT_PATH_SEPARATOR
Default path separator: "/".- See Also:
-
-
Constructor Details
-
AntPathMatcher
public AntPathMatcher()使用DEFAULT_PATH_SEPARATOR作为分隔符构造 -
AntPathMatcher
使用自定义的分隔符构造- Parameters:
pathSeparator- the path separator to use, must not benull.
-
-
Method Details
-
setPathSeparator
设置路径分隔符- Parameters:
pathSeparator- 分隔符,null表示使用默认分隔符DEFAULT_PATH_SEPARATOR- Returns:
- this
-
setCaseSensitive
设置是否大小写敏感,默认为true- Parameters:
caseSensitive- 是否大小写敏感- Returns:
- this
-
setTrimTokens
设置是否去除路径节点两边的空白符,默认为false- Parameters:
trimTokens- 是否去除路径节点两边的空白符- Returns:
- this
-
setCachePatterns
Specify whether to cache parsed pattern metadata for patterns passed into this matcher'smatch(java.lang.String, java.lang.String)method. A value oftrueactivates an unlimited pattern cache; a value offalseturns the pattern cache off completely.Default is for the cache to be on, but with the variant to automatically turn it off when encountering too many patterns to cache at runtime (the threshold is 65536), assuming that arbitrary permutations of patterns are coming in, with little chance for encountering a recurring pattern.
- Parameters:
cachePatterns- 是否缓存表达式- Returns:
- this
- See Also:
-
isPattern
判断给定路径是否是表达式- Parameters:
path- 路径- Returns:
- 是否为表达式
-
match
给定路径是否匹配表达式- Parameters:
pattern- 表达式path- 路径- Returns:
- 是否匹配
-
matchStart
前置部分匹配- Parameters:
pattern- 表达式path- 路径- Returns:
- 是否匹配
-
doMatch
protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) 执行匹配,判断给定的path是否匹配pattern- Parameters:
pattern- 表达式path- 路径fullMatch- 是否全匹配。true表示全路径匹配,false表示只匹配开始uriTemplateVariables- 变量映射- Returns:
true表示提供的path匹配,false表示不匹配
-
tokenizePattern
Tokenize the given path pattern into parts, based on this matcher's settings.Performs caching based on
setCachePatterns(boolean), delegating totokenizePath(String)for the actual tokenization algorithm.- Parameters:
pattern- the pattern to tokenize- Returns:
- the tokenized pattern parts
-
tokenizePath
Tokenize the given path into parts, based on this matcher's settings.- Parameters:
path- the path to tokenize- Returns:
- the tokenized path parts
-
getStringMatcher
Build or retrieve anAntPathMatcher.AntPathStringMatcherfor the given pattern.The default implementation checks this AntPathMatcher's internal cache (see
setCachePatterns(boolean)), creating a new AntPathStringMatcher instance if no cached copier is found.When encountering too many patterns to cache at runtime (the threshold is 65536), it turns the default cache off, assuming that arbitrary permutations of patterns are coming in, with little chance for encountering a recurring pattern.
This method may be overridden to implement a custom cache strategy.
- Parameters:
pattern- the pattern to match against (nevernull)- Returns:
- a corresponding AntPathStringMatcher (never
null) - See Also:
-
extractPathWithinPattern
Given a pattern and a full path, determine the pattern-mapped part.For example:
- '
/docs/cvs/commit.html' and '/docs/cvs/commit.html→ '' - '
/docs/*' and '/docs/cvs/commit→ 'cvs/commit' - '
/docs/cvs/*.html' and '/docs/cvs/commit.html→ 'commit.html' - '
/docs/**' and '/docs/cvs/commit→ 'cvs/commit' - '
/docs/**\/*.html' and '/docs/cvs/commit.html→ 'cvs/commit.html' - '
/*.html' and '/docs/cvs/commit.html→ 'docs/cvs/commit.html' - '
*.html' and '/docs/cvs/commit.html→ '/docs/cvs/commit.html' - '
*' and '/docs/cvs/commit.html→ '/docs/cvs/commit.html'
Assumes that
match(java.lang.String, java.lang.String)returnstruefor 'pattern' and 'path', but does not enforce this.- Parameters:
pattern- 表达式path- 路径- Returns:
- 表达式匹配到的部分
- '
-
extractUriTemplateVariables
提取参数- Parameters:
pattern- 模式path- 路径- Returns:
- 参数
-
combine
Combine two patterns into a new pattern. This implementation simply concatenates the two patterns, unless the first pattern contains a file extension match (e.g.,*.html). In that case, the second pattern will be merged into the first. Otherwise, anIllegalArgumentExceptionwill be thrown.- Parameters:
pattern1- the first patternpattern2- the second pattern- Returns:
- the combination of the two patterns
- Throws:
IllegalArgumentException- if the two patterns cannot be combined
-
getPatternComparator
Given a full path, returns aComparatorsuitable for sorting patterns in order of explicitness.This
Comparatorwill sort a list so that more specific patterns (without URI templates or wild cards) come before generic patterns. So given a list with the following patterns, the returned compare will sort this list so that the order will be as indicated./hotels/new/hotels/{hotel}/hotels/*
The full path given as parameter is used to test for exact matches. So when the given path is
/hotels/2, the pattern/hotels/2will be sorted before/hotels/1.- Parameters:
path- the full path to use for comparison- Returns:
- a compare capable of sorting patterns in order of explicitness
-