Spring工具类--路径匹配(AntPathMatcher)--使用/实例
2023-12-24 16:59 l_v_y_forever 阅读(962) 评论(0) 编辑 收藏 举报原文网址:Spring工具类--路径匹配(AntPathMatcher)--使用/实例_IT利刃出鞘的博客-CSDN博客
简介
整个Spring(SpringBoot)框架的路径解析都是按照Ant的风格来的,比如:Controller的请求路径、文件路径、包的路径。所以,掌握Ant的路径匹配很重要。
Spring中的具体实现: org.springframework.util.AntPathMatcher。其注释里边有解释,翻译成中文如下表:
符号 |
作用 |
示例 |
? |
匹配一个字符。 不能匹配目录:这个字符不能是代表路径分隔符的/ |
/dir/app? 匹配:/dir/app1、/dir/app2 不匹配:/dir/app、/dir/app12、index/ |
* |
匹配0到多个字符。 |
/dir/app* 匹配:/dir/app、/dir/app1、/dir/app12、/dir/appa/ 不匹配:/dir/app/a |
** |
匹配多级目录。 |
/dir/**/app* 匹配:/dir/xxx/app* /dir/xxx/yyy/app*
/dir/app/** 匹配:dir/app/aa/bcd/e |
{spring:[a-z]+} |
将正则表达式[a-z]+匹配到的值,赋值给名为 spring 的路径变量。 必须是完全匹配才行,在SpringMVC中只有完全匹配才会进入controller层的方法 |
@RequestMapping("/index/{username:[a-b]+}") 结果 index/ab true 输出 ab |
大全(有实例)
摘录自Spring的官网测试,不得不说 Spring 的测试用例写的实在是太完善了。 其网址为:https://github.com/spring-projects/spring-framework/blob/main/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java
其测试太长了,本处将其分类。下边这行是公共代码:
private final AntPathMatcher pathMatcher = new AntPathMatcher();
match
-
// test exact matching
-
assertThat(pathMatcher.match("test", "test")).isTrue();
-
assertThat(pathMatcher.match("/test", "/test")).isTrue();
-
// SPR-14141
-
assertThat(pathMatcher.match("https://example.org", "https://example.org")).isTrue();
-
assertThat(pathMatcher.match("/test.jpg", "test.jpg")).isFalse();
-
assertThat(pathMatcher.match("test", "/test")).isFalse();
-
assertThat(pathMatcher.match("/test", "test")).isFalse();
-
-
// test matching with ?'s
-
assertThat(pathMatcher.match("t?st", "test")).isTrue();
-
assertThat(pathMatcher.match("??st", "test")).isTrue();
-
assertThat(pathMatcher.match("tes?", "test")).isTrue();
-
assertThat(pathMatcher.match("te??", "test")).isTrue();
-
assertThat(pathMatcher.match("?es?", "test")).isTrue();
-
assertThat(pathMatcher.match("tes?", "tes")).isFalse();
-
assertThat(pathMatcher.match("tes?", "testt")).isFalse();
-
assertThat(pathMatcher.match("tes?", "tsst")).isFalse();
-
-
// test matching with *'s
-
assertThat(pathMatcher.match("*", "test")).isTrue();
-
assertThat(pathMatcher.match("test*", "test")).isTrue();
-
assertThat(pathMatcher.match("test*", "testTest")).isTrue();
-
assertThat(pathMatcher.match("test/*", "test/Test")).isTrue();
-
assertThat(pathMatcher.match("test/*", "test/t")).isTrue();
-
assertThat(pathMatcher.match("test/*", "test/")).isTrue();
-
assertThat(pathMatcher.match("*test*", "AnothertestTest")).isTrue();
-
assertThat(pathMatcher.match("*test", "Anothertest")).isTrue();
-
assertThat(pathMatcher.match("*.*", "test.")).isTrue();
-
assertThat(pathMatcher.match("*.*", "test.test")).isTrue();
-
assertThat(pathMatcher.match("*.*", "test.test.test")).isTrue();
-
assertThat(pathMatcher.match("test*aaa", "testblaaaa")).isTrue();
-
assertThat(pathMatcher.match("test*", "tst")).isFalse();
-
assertThat(pathMatcher.match("test*", "tsttest")).isFalse();
-
assertThat(pathMatcher.match("test*", "test/")).isFalse();
-
assertThat(pathMatcher.match("test*", "test/t")).isFalse();
-
assertThat(pathMatcher.match("test/*", "test")).isFalse();
-
assertThat(pathMatcher.match("*test*", "tsttst")).isFalse();
-
assertThat(pathMatcher.match("*test", "tsttst")).isFalse();
-
assertThat(pathMatcher.match("*.*", "tsttst")).isFalse();
-
assertThat(pathMatcher.match("test*aaa", "test")).isFalse();
-
assertThat(pathMatcher.match("test*aaa", "testblaaab")).isFalse();
-
-
// test matching with ?'s and /'s
-
assertThat(pathMatcher.match("/?", "/a")).isTrue();
-
assertThat(pathMatcher.match("/?/a", "/a/a")).isTrue();
-
assertThat(pathMatcher.match("/a/?", "/a/b")).isTrue();
-
assertThat(pathMatcher.match("/??/a", "/aa/a")).isTrue();
-
assertThat(pathMatcher.match("/a/??", "/a/bb")).isTrue();
-
assertThat(pathMatcher.match("/?", "/a")).isTrue();
-
-
// test matching with **'s
-
assertThat(pathMatcher.match("/**", "/testing/testing")).isTrue();
-
assertThat(pathMatcher.match("/*/**", "/testing/testing")).isTrue();
-
assertThat(pathMatcher.match("/**/*", "/testing/testing")).isTrue();
-
assertThat(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla")).isTrue();
-
assertThat(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla/bla")).isTrue();
-
assertThat(pathMatcher.match("/**/test", "/bla/bla/test")).isTrue();
-
assertThat(pathMatcher.match("/bla/**/**/bla", "/bla/bla/bla/bla/bla/bla")).isTrue();
-
assertThat(pathMatcher.match("/bla*bla/test", "/blaXXXbla/test")).isTrue();
-
assertThat(pathMatcher.match("/*bla/test", "/XXXbla/test")).isTrue();
-
assertThat(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test")).isFalse();
-
assertThat(pathMatcher.match("/*bla/test", "XXXblab/test")).isFalse();
-
assertThat(pathMatcher.match("/*bla/test", "XXXbl/test")).isFalse();
-
-
assertThat(pathMatcher.match("/????", "/bala/bla")).isFalse();
-
assertThat(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb")).isFalse();
-
-
assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/")).isTrue();
-
assertThat(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing")).isTrue();
-
assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();
-
assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg")).isTrue();
-
-
assertThat(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing/")).isTrue();
-
assertThat(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing")).isTrue();
-
assertThat(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();
-
assertThat(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing/testing")).isFalse();
-
-
assertThat(pathMatcher.match("/x/x/**/bla", "/x/x/x/")).isFalse();
-
-
assertThat(pathMatcher.match("/foo/bar/**", "/foo/bar")).isTrue();
-
-
assertThat(pathMatcher.match("", "")).isTrue();
-
-
assertThat(pathMatcher.match("/{bla}.*", "/testing.html")).isTrue();
-
assertThat(pathMatcher.match("/{bla}", "//x\ny")).isTrue();
matchWithNullPath
转载自:https://blog.csdn.net/feiying0canglang/article/details/120678900
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!