Spring URI parsing utility

I would like to extract the path variables and request parameters from the URL using existing Spring functions. I have a path format string that is valid for MVC @RequestMapping or UriComponentsBuilder . I also have a real way. I would like to extract path variables from this path.

For instance.

 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")); 

This is a bit of a converse to UriComponentsBuilder , which from my reading of Javadocs does not have any parsing functions.

+6
source share
2 answers

Here he is:

  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")); 
+9
source

The first thing to look is the source code for org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver is the converter used to annotate @PathVariable in MVC. Take a look at the resolveName method to see what Spring code does. There you can find a class that uses MVC. Then you can see if you can satisfy his requirements.

0
source

Source: https://habr.com/ru/post/976686/


All Articles