用于解析URI的Spring实用程序 [英] Spring utility for parsing URIs
本文介绍了用于解析URI的Spring实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用现有的Spring功能从URL中提取路径变量和查询参数。我有一个路径格式字符串,它对MVC @RequestMapping
或 UriComponentsBuilder
有效。我也有一条实际的道路。我想从该路径中提取路径变量。
I would like to extract path variables and query parameters from a URL using existing Spring features. I have a path format string which is valid for an MVC @RequestMapping
or UriComponentsBuilder
. I also have an actual path. I would like to extract path variables from that path.
例如。
String format = "location/{state}/{city}";
String actualUrl = "location/washington/seattle";
TheThingImLookingFor parser = new TheThingImLookingFor(format);
Map<String, String> variables = parser.extractPathVariables(actualUrl);
assertThat(variables.get("state", is("washington"));
assertThat(variables.get("city", is("seattle"));
它类似于 UriComponentsBuilder
的反转,这是我读到的Javadocs没有任何解析功能。
It is something like the inverse of UriComponentsBuilder
, which from my reading of the Javadocs does not have any parsing features.
推荐答案
这就是:
String format = "location/{state}/{city}";
String actualUrl = "location/washington/seattle";
AntPathMatcher pathMatcher = new AntPathMatcher();
Map<String, String> variables = pathMatcher.extractUriTemplateVariables(format, actualUrl);
assertThat(variables.get("state"), is("washington"));
assertThat(variables.get("city"), is("seattle"));
这篇关于用于解析URI的Spring实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文