Spring utility for parsing URIs

后端 未结 2 1599
感动是毒
感动是毒 2021-01-02 13:37

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 @RequestMa

相关标签:
2条回答
  • 2021-01-02 13:55

    The first place to look is in the source code for org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver this is the resolver used for the @PathVariable annotation in MVC. Look at the method "resolveName" to see what the Spring code is doing. Form there you should be able to find the class that MVC is using. Then you can see if you can make it meet your requirements.

    0 讨论(0)
  • 2021-01-02 14:11

    Here it goes:

        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"));
    
    0 讨论(0)
提交回复
热议问题