@Test
public void test01() {
AntPathMatcher pathMatcher = new AntPathMatcher();
boolean match1 = pathMatcher.match("/user/getUserById/{userId}", "/user/getUserById/3");
Map<String, String> pathParamMap1 = pathMatcher.extractUriTemplateVariables("/user/getUserById/{userId}", "/user/getUserById/3");
boolean match2 = pathMatcher.match("/user/getUserById/{userId}", "/user/getUserById");
System.out.println(match1); // true
System.out.println(pathParamMap1); // {userId=3}
System.out.println(match2); // false
}
@Test
public void test02() {
AntPathMatcher pathMatcher = new AntPathMatcher();
boolean match1 = pathMatcher.match("/user/getUserById/*", "/user/getUserById/3");
Map<String, String> pathParamMap1 = pathMatcher.extractUriTemplateVariables("/user/getUserById/{userId}", "/user/getUserById/3");
boolean match11 = pathMatcher.match("/user/getUserById/**", "/user/getUserById/3");
Map<String, String> pathParamMap11 = pathMatcher.extractUriTemplateVariables("/user/getUserById/**", "/user/getUserById/3");
String s11 = pathMatcher.extractPathWithinPattern("/user/getUserById/**", "/user/getUserById/3");
boolean match12 = pathMatcher.match("/user/getUserById/**", "/user/getUserById/a/b");
String s12 = pathMatcher.extractPathWithinPattern("/user/getUserById/**", "/user/getUserById/a/b");
boolean match13 = pathMatcher.match("/user/getUserById/*", "/user/getUserById/a/b");
boolean match2 = pathMatcher.match("/user/getUserById/{userId}", "/user/getUserById");
System.out.println(match1); // true
System.out.println(pathParamMap1); // {userId=3}
System.out.println(match11); // true
System.out.println(pathParamMap11); // {}
System.out.println(s11); // 3
System.out.println(match12); // true
System.out.println(s12); // a/b
System.out.println(match13); // false
System.out.println(match2); // false
}
@Test
public void test03() {
AntPathMatcher pathMatcher = new AntPathMatcher();
boolean m1 = pathMatcher.match("/a/*/c", "/a/b/c");
String s1 = pathMatcher.extractPathWithinPattern("/a/*/c", "/a/b/c");
boolean m2 = pathMatcher.match("/a/*/c", "/a/b/bb/c");
System.out.println(m1); // true
System.out.println(s1); // b/c
System.out.println(m2); // false
}
@Test
public void test04() {
AntPathMatcher pathMatcher = new AntPathMatcher();
boolean m1 = pathMatcher.match("/a/**", "global/a/b/c");
boolean m2 = pathMatcher.match("/a/**", "a/b/c");
boolean m3 = pathMatcher.match("a/**", "/a/b/c");
boolean m4 = pathMatcher.match("a/**", "a/b/c");
boolean m5 = pathMatcher.match("/a/**", "/a/b/c");
System.out.println(m1); // false
System.out.println(m2); // false
System.out.println(m3); // false
System.out.println(m4); // true
System.out.println(m5); // true
}
原文:https://www.cnblogs.com/leftthen/p/5212221.html、
扩展:AntPathMatcher路径匹配器,Ant风格的URLhttps://blog.csdn.net/f641385712/article/details/118032869
AntPathMatcher如名使用的ant 的匹配规则,我们先看看吧.
字符wildcard 描述
? 匹配一个字符
* 匹配0个及以上字符
** 匹配0个及以上目录directories
看几个官方的例子吧:
com/t?st.jsp - 匹配: com/test.jsp , com/tast.jsp , com/txst.jsp
com/*.jsp - 匹配: com文件夹下的全部.jsp文件
com/**/test.jsp - 匹配: com文件夹和子文件夹下的全部.jsp文件,
org/springframework/**/*.jsp 匹配: org/springframework文件夹和子文件夹下的全部.jsp文件
org/**/servlet/bla.jsp - 匹配: org/springframework/servlet/bla.jsp , org/springframework/testing/servlet/bla.jsp , org/servlet/bla.jsp
主要是判断是否匹配pattern,并解析出path中的参数
package org.springframework.util;
public interface PathMatcher {
/**
* 判断传入的path是否可以作为pattern使用
*/
boolean isPattern(String path);
/**
* 使用pattern匹配path
*/
boolean match(String pattern, String path);
/**
* 如名,是否开始部分匹配
*/
boolean matchStart(String pattern, String path);
/**
* 提取path中匹配到的部分,如pattern(myroot/*.html),path(myroot/myfile.html),返回myfile.html
*/
String extractPathWithinPattern(String pattern, String path);
/**
* 提取path中匹配到的部分,只是这边还需跟占位符配对为map,
* 如pattern(/hotels/{hotel}),path(/hotels/1),解析出"hotel"->"1"
*/
Map<String, String> extractUriTemplateVariables(String pattern, String path);
/**
* 提供比较器
*/
Comparator<String> getPatternComparator(String path);
/**
* 合并pattern,pattern1然后pattern2
*/
String combine(String pattern1, String pattern2);
}
package org.springframework.util;
public class AntPathMatcherTests {
@Test
public void match() {
// ...
assertFalse(pathMatcher.match("/x/x/**/bla", "/x/x/x/"));
// ...
}
@Test
public void withMatchStart() {
// ...
assertTrue(pathMatcher.matchStart("/x/x/**/bla", "/x/x/x/"));
// ...
}
}
package org.springframework.util;
public class AntPathMatcherTests {
@Test
public void extractPathWithinPattern() throws Exception {
// ...
assertEquals("", pathMatcher.extractPathWithinPattern("/docs/commit.html", "/docs/commit.html"));
assertEquals("cvs/commit", pathMatcher.extractPathWithinPattern("/docs/*", "/docs/cvs/commit"));
assertEquals("docs/cvs/commit", pathMatcher.extractPathWithinPattern("/d?cs/*", "/docs/cvs/commit"));
// ...
}
}
package org.springframework.util;
public class AntPathMatcherTests {
@Test
public void extractUriTemplateVariables() throws Exception {
Map<String, String> result = pathMatcher.extractUriTemplateVariables("/hotels/{hotel}", "/hotels/1");
assertEquals(Collections.singletonMap("hotel", "1"), result);
// ...
result = pathMatcher.extractUriTemplateVariables("/{page}.*", "/42.html");
assertEquals(Collections.singletonMap("page", "42"), result);
// ...
}
/**
* SPR-7787
*/
@Test
public void extractUriTemplateVarsRegexQualifiers() {
Map<String, String> result = pathMatcher.extractUriTemplateVariables(
"{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar",
"com.example-sources-1.0.0.jar");
assertEquals("com.example", result.get("symbolicName"));
assertEquals("1.0.0", result.get("version"));
// ...
}
}
package org.springframework.util;
public class AntPathMatcherTests {
@Test
public void combine() {
// ...
assertEquals("/hotels", pathMatcher.combine("/hotels", null));
assertEquals("/hotels/booking", pathMatcher.combine("/hotels/*", "/booking"));
assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "booking"));
assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "/booking"));
assertEquals("/hotels/booking", pathMatcher.combine("/hotels", "/booking"));
assertEquals("/hotels/{hotel}", pathMatcher.combine("/hotels/*", "{hotel}"));
assertEquals("/hotels/**/{hotel}", pathMatcher.combine("/hotels/**", "{hotel}"));
assertEquals("/hotels/*/booking/{booking}", pathMatcher.combine("/hotels/*/booking", "{booking}"));
}
}