Spring Ant Style PathMatcher简介
一、前言
- Spring默认的策略实现了
org.springframework.util.AntPathMatcher
,即Apache Ant风格的路径匹配规则,整个Spring框架的路径解析都是按照Ant风格来解析的。 - AntPathMatcher不仅可以匹配Spring的
@RequestMapping
路径,也可以用来匹配各种字符串,包括文件路径等。
二、基本规则
Apache Ant风格的路径有三种通配符匹配方法(在下面的表格中列出)
通配符 | 描述 |
---|---|
? | 匹配任何单字符 |
* | 匹配0或者多个字符 |
** | 匹配0或者更多的目录 |
**最长匹配规则(has more characters),即越精确的模式越会被优先匹配到。例如,URL请求/app/dir/file.jsp,现在存在两个路径匹配模式//.jsp和/app/dir/.jsp,那么会根据模式/app/dir/*.jsp来匹配。
三、实例
Path | Description |
---|---|
/app/*.x | 匹配所有在app路径下的带.x文件后缀的文件 |
/app/p?ttern | 匹配 /app/pattern 和 /app/pXttern,但是不包括 /app/pttern |
/**/example | 匹配 /app/example, /app/foo/example, 和 /example |
/app/**/dir/file. | 匹配 /app/dir/file.jsp, /app/foo/dir/file.html,/app/foo/bar/dir/file.pdf, 和 /app/dir/file.java |
/**/*.jsp | 匹配所有的.jsp 文件 |
参考资料
[1]:Ant路径匹配规则AntPathMatcher的注意事项
[2]:Apache Ant
来源:CSDN
作者:JavaCoder567
链接:https://blog.csdn.net/qq_40151840/article/details/104607683