用于解析URI的Spring实用程序

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

例如。

 String format = "location/{state}/{city}"; String actualUrl = "location/washington/seattle"; TheThingImLookingFor parser = new TheThingImLookingFor(format); Map variables = parser.extractPathVariables(actualUrl); assertThat(variables.get("state", is("washington")); assertThat(variables.get("city", is("seattle")); 

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

它来了:

  String format = "location/{state}/{city}"; String actualUrl = "location/washington/seattle"; AntPathMatcher pathMatcher = new AntPathMatcher(); Map variables = pathMatcher.extractUriTemplateVariables(format, actualUrl); assertThat(variables.get("state"), is("washington")); assertThat(variables.get("city"), is("seattle")); 

首先要看的是org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver的源代码,这是用于MVC中@PathVariable注释的解析器。 查看方法“resolveName”以查看Spring代码正在执行的操作。 从那里你应该能够找到MVC正在使用的类。 然后你可以看看你是否能满足你的要求。