Spring工具类-路径匹配(AntPathMatcher)

发布时间 2023-11-24 16:36:25作者: coder_Fish

 

1.介绍

Spring(SpringBoot)框架的路径解析都是按照Ant的风格。

Spring中的具体实现: org.springframework.util.AntPathMatcher

?  匹配1个字符    /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*

{spring:[a-z]+}  将正则表达式[a-z]+匹配到的值,赋值给名为 spring 的路径变量。

@RequestMapping("/index/{name:[a-b]+}")
@ResponseBody
public String index(@PathVariable("name") String name){
    System.out.println(name);
    return name;
}

 

2.使用

private final AntPathMatcher pathMatcher = new AntPathMatcher();
pathMatcher.match("/a/a/**/bla", "/a/a/a/");