public class AntPathMatcher extends Object
匹配URL的规则如下:
?
匹配单个字符*
匹配0个或多个字符**
0个或多个路径中的目录节点{hutool:[a-z]+}
匹配以"hutool"命名的正则 [a-z]+
例子:
com/t?st.jsp
— 匹配 com/test.jsp
或 com/tast.jsp
或 com/txst.jsp
com/*.jsp
— 匹配com
目录下全部 .jsp
文件com/**/test.jsp
— 匹配com
目录下全部 test.jsp
文件cn/hutool/**/*.jsp
— 匹配cn/hutool
路径下全部.jsp
文件org/**/servlet/bla.jsp
— 匹配cn/hutool/servlet/bla.jsp
或cn/hutool/testing/servlet/bla.jsp
或 org/servlet/bla.jsp
com/{filename:\\w+}.jsp
匹配 com/test.jsp
并将 test
关联到 filename
变量注意: 表达式和路径必须都为绝对路径或都为相对路径。
Modifier and Type | Class and Description |
---|---|
protected static class |
AntPathMatcher.AntPathStringMatcher
Tests whether or not a string matches against a pattern via a
Pattern . |
protected static class |
AntPathMatcher.AntPatternComparator
The default
Comparator implementation returned by
getPatternComparator(String) . |
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_PATH_SEPARATOR
Default path separator: "/".
|
Constructor and Description |
---|
AntPathMatcher()
使用
DEFAULT_PATH_SEPARATOR 作为分隔符构造 |
AntPathMatcher(String pathSeparator)
使用自定义的分隔符构造
|
Modifier and Type | Method and Description |
---|---|
String |
combine(String pattern1,
String pattern2)
Combine two patterns into a new pattern.
|
protected boolean |
doMatch(String pattern,
String path,
boolean fullMatch,
Map<String,String> uriTemplateVariables)
执行匹配,判断给定的
path 是否匹配pattern |
String |
extractPathWithinPattern(String pattern,
String path)
Given a pattern and a full path, determine the pattern-mapped part.
|
Map<String,String> |
extractUriTemplateVariables(String pattern,
String path) |
Comparator<String> |
getPatternComparator(String path)
Given a full path, returns a
Comparator suitable for sorting patterns in order of
explicitness. |
protected AntPathMatcher.AntPathStringMatcher |
getStringMatcher(String pattern)
Build or retrieve an
AntPathMatcher.AntPathStringMatcher for the given pattern. |
boolean |
isPattern(String path)
判断给定路径是否是表达式
|
boolean |
match(String pattern,
String path)
给定路径是否匹配表达式
|
boolean |
matchStart(String pattern,
String path)
前置部分匹配
|
AntPathMatcher |
setCachePatterns(boolean cachePatterns)
Specify whether to cache parsed pattern metadata for patterns passed
into this matcher's
match(java.lang.String, java.lang.String) method. |
AntPathMatcher |
setCaseSensitive(boolean caseSensitive)
设置是否大小写敏感,默认为
true |
AntPathMatcher |
setPathSeparator(String pathSeparator)
设置路径分隔符
|
AntPathMatcher |
setTrimTokens(boolean trimTokens)
设置是否去除路径节点两边的空白符,默认为
false |
protected 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.
|
public static final String DEFAULT_PATH_SEPARATOR
public AntPathMatcher()
DEFAULT_PATH_SEPARATOR
作为分隔符构造public AntPathMatcher(String pathSeparator)
pathSeparator
- the path separator to use, must not be null
.public AntPathMatcher setPathSeparator(String pathSeparator)
pathSeparator
- 分隔符,null
表示使用默认分隔符DEFAULT_PATH_SEPARATOR
public AntPathMatcher setCaseSensitive(boolean caseSensitive)
true
caseSensitive
- 是否大小写敏感public AntPathMatcher setTrimTokens(boolean trimTokens)
false
trimTokens
- 是否去除路径节点两边的空白符public AntPathMatcher setCachePatterns(boolean cachePatterns)
match(java.lang.String, java.lang.String)
method. A value of true
activates an unlimited pattern cache; a value of false
turns
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.
cachePatterns
- 是否缓存表达式getStringMatcher(String)
public boolean isPattern(String path)
path
- 路径public boolean match(String pattern, String path)
pattern
- 表达式path
- 路径public boolean matchStart(String pattern, String path)
pattern
- 表达式path
- 路径protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String,String> uriTemplateVariables)
path
是否匹配pattern
pattern
- 表达式path
- 路径fullMatch
- 是否全匹配。true
表示全路径匹配,false
表示只匹配开始uriTemplateVariables
- 变量映射true
表示提供的 path
匹配, false
表示不匹配protected String[] tokenizePattern(String pattern)
Performs caching based on setCachePatterns(boolean)
, delegating to
tokenizePath(String)
for the actual tokenization algorithm.
pattern
- the pattern to tokenizeprotected String[] tokenizePath(String path)
path
- the path to tokenizeprotected AntPathMatcher.AntPathStringMatcher getStringMatcher(String pattern)
AntPathMatcher.AntPathStringMatcher
for the given pattern.
The default implementation checks this AntPathMatcher's internal cache
(see setCachePatterns(boolean)
), creating a new AntPathStringMatcher instance
if no cached copy 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.
pattern
- the pattern to match against (never null
)null
)setCachePatterns(boolean)
public String extractPathWithinPattern(String pattern, String path)
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)
returns true
for 'pattern
' and 'path
', but
does not enforce this.
pattern
- 表达式path
- 路径public Map<String,String> extractUriTemplateVariables(String pattern, String path)
public String combine(String pattern1, String pattern2)
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,
an IllegalArgumentException
will be thrown.
Examples
Pattern 1 | Pattern 2 | Result |
---|---|---|
null | null | |
/hotels | null | /hotels |
null | /hotels | /hotels |
/hotels | /bookings | /hotels/bookings |
/hotels | bookings | /hotels/bookings |
/hotels/* | /bookings | /hotels/bookings |
/hotels/** | /bookings | /hotels/**/bookings |
/hotels | {hotel} | /hotels/{hotel} |
/hotels/* | {hotel} | /hotels/{hotel} |
/hotels/** | {hotel} | /hotels/**/{hotel} |
/*.html | /hotels.html | /hotels.html |
/*.html | /hotels | /hotels.html |
/*.html | /*.txt | IllegalArgumentException |
pattern1
- the first patternpattern2
- the second patternIllegalArgumentException
- if the two patterns cannot be combinedpublic Comparator<String> getPatternComparator(String path)
Comparator
suitable for sorting patterns in order of
explicitness.
This Comparator
will 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 comparator
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/2
will be sorted before /hotels/1
.
path
- the full path to use for comparisonCopyright © 2024. All rights reserved.