用于解析URI的Spring实用程序

Dav*_*d V 7 java spring spring-mvc

我想使用现有的Spring功能从URL中提取路径变量和查询参数.我有一个路径格式字符串,对MVC @RequestMappingUriComponentsBuilder.我也有一条实际的道路.我想从该路径中提取路径变量.

例如.

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"));
Run Code Online (Sandbox Code Playgroud)

它类似于反转UriComponentsBuilder,从我对Javadocs的阅读中没有任何解析功能.

Bij*_*men 11

在这里:

    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"));
Run Code Online (Sandbox Code Playgroud)