Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring MVC URI Template processing implement RFC 6570?

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?

like image 897
Alexandre Roger Avatar asked Jun 28 '26 09:06

Alexandre Roger


2 Answers

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

}
like image 129
Jose Luis Martin Avatar answered Jul 01 '26 06:07

Jose Luis Martin


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

like image 40
dan carter Avatar answered Jul 01 '26 04:07

dan carter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!