質問

I am trying to find a definitive answer as to how URI Templates in Spring MVC are parsed. The Documentation links to the Draft RFC, but Spring is not listed in the RFC's implementations page On the other hand, this page seems to suggest that they use SpEL. Anyone has a link that could clear this for me?

役に立ちましたか?

解決

There are two classes involved on parsing URI Templates.

  • AntPathMatcher.extractUriTemplateVariables(): Used in @RequestMapping MVC annotation
  • UriTemplate : Used in RestTemplate client.

You can use these classes to test how the URI Templates are parsed.

For example for MVC RequestMapping:

@RunWith(BlockJUnit4ClassRunner.class)
public class UriTemplateTest {

    private AntPathMatcher matcher = new AntPathMatcher();
    private Log log = LogFactory.getLog(UriTemplateTest.class);

    @Test
    public void testUriTemplate() {
        Map<String, String> variables = matcher.extractUriTemplateVariables("/booking/hotel/{id}", "/booking/hotel/43");
        log.info(variables);
    }

}

他のヒント

Spring does not support RFC6570,

For instance the first passes and the second fails.

@Test
public void thatUriPathVariablesWithSlashGetEncoded() {
    String uri = com.damnhandy.uri.template.UriTemplate.fromTemplate("http://localhost:80/context/entity/{var1}/{var2}").set("var1", "my/Id").set("var2", "blah").expand();

    Assert.assertThat(uri, Matchers.is("http://localhost:80/context/entity/my%2FId/blah"));
}

@Test
public void thatUriPathVariablesWithSlashGetEncodedWithSpring() {
    String uri = new org.springframework.web.util.UriTemplate("http://localhost:80/context/entity/{var1}/{var2}").expand("my/Id", "blah")
            .toASCIIString();

    Assert.assertThat(uri, Matchers.is("http://localhost:80/context/entity/my%2FId/blah"));
}

There is a ticket open to add support one day. https://jira.spring.io/browse/SPR-10505

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top