文章目录

代码

1
2
3
4
5
6
7
public static void main(String[] args){
AntPathMatcher matcher = new AntPathMatcher();
Map<String,String> map = matcher.extractUriTemplateVariables("/dev/{id}","/dev/123");
map.entrySet().forEach(k->{
System.out.println(k.getKey() + ":" + k.getValue());
});
}

输出结果

1
id:123

AntPathMatcher 是 Spring core 提供的一个工具类。使用这个工具类我们可以很容易的实现URL里变量的提取。 我们在spring里常见的代码是:

1
2
@RequestMapping(value = "/id/{fileId}", method = RequestMethod.GET)
public String getFileById(@PathVariable(value = "fileId", required = true) String fileId)

通过AntPathMatcher 就可以自己实现URL里的变量提取了。

文章目录