Spring MVC URI模板处理是否实现了RFC 6570?

时间:2013-10-17 14:48:16

标签: spring rfc spring-el uritemplate

我试图找到一个关于如何解析Spring MVC中的URI模板的明确答案。 Documentation指向RFC草案的链接,但RFC's implementations page中未列出Spring 另一方面,this page似乎暗示他们使用SpEL。 任何人都有链接可以为我清除这个?

2 个答案:

答案 0 :(得分:1)

解析URI模板涉及两个类。

  • AntPathMatcher.extractUriTemplateVariables():用于@RequestMapping MVC注释
  • UriTemplate:用于RestTemplate客户端。

您可以使用这些类来测试如何解析URI模板。

例如对于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);
    }

}

答案 1 :(得分:1)

Spring不支持RFC6570,

例如第一次通过,第二次通过。

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

有一张门票可以在一天内添加支持。 https://jira.spring.io/browse/SPR-10505