【问题标题】:URI template needs to match with variable value that is a set of foldersURI 模板需要与一组文件夹的变量值匹配
【发布时间】:2013-05-28 21:39:54
【问题描述】:

我正在使用 org.springframework.web.util.UriTemplate 并且我正在尝试匹配这个 uri 模板:

http://{varName1}/path1/path2/{varName2}/{varName3}/{varName4}

使用以下 uri:

http://hostname/path1/path2/design/99999/product/schema/75016TC806AA/TC806AA.tar

目前我得到以下 uri 变量:

{varName1=hostname, varName2=design/99999/product/schema, varName3=75016TC806AA,varName4=TC806AA.tar}

但我想得到以下 uri 变量:

{varName1=hostname, varName2=design varName3=99999, varName4=product/schema/75016TC806AA/TC806AA.tar}

我尝试在模板中使用通配符作为 * 或 +,但这似乎不起作用:

http://{varName1}/path1/path2/{varName2}/{varName3}/{varName4*}

http://{varName1}/path1/path2/{varName2}/{varName3}/{+varName4}

已编辑

String url = http://localhost/path1/path2/folder1/folder2/folder3/folder4/folder5
UriTemplate uriTemplate = new UriTemplate(urlTemplateToMatch);
Map<String, String> uriVariables = uriTemplate.match(url);

String urlTemplateToMatch1 = http://{varName1}/path1/path2/{varName2}/{varName3}/{varName4}
uriVariables1 = {varName1=localhost, varName2=folder1/folder2/folder3, varName3=folder4, varName4=folder5}

String urlTemplateToMatch2 = http://{varName1}/test1/test2/{varName2:.*?}/{varName3:.*?}/{varName4}
uriVariables2 = {varName1=localhost, varName2:.*?=folder1/folder2/folder3, varName3:.*?=folder4, varName4=folder5}

String urlTemplateToMatch3 = http://{varName1}/test1/test2/{varName2:\\w*}/{varName3:.\\w*}/{varName4}
uriVariables3 = {varName1=localhost, varName2:\w*=folder1/folder2/folder3, varName3:\w*=folder4, varName4=folder5}

【问题讨论】:

  • 查看 URITemplate 的代码我看不到任何可能对您有帮助的东西。我唯一的想法是通过 varName4 之前的附加 path3 元素来澄清 url,例如 http://{varName1}/path1/path2/{varName2}/{varName3}/path3/{varName4} 或编写自己的调整版本。 ..
  • 由于我无法更改 URITemplate,因此无法选择您的解决方案。
  • 您使用的是什么版本?。我为 3.1.1 版本添加了一个测试用例。
  • 我使用的是 3.0.4 版本。我想这就是为什么这在我的项目中不起作用的原因。
  • 不,这两个版本之间的 UriTemplate 类没有变化。

标签: java spring uritemplate


【解决方案1】:

尝试:

http://{varName1}/path1/path2/{varName2:.*?}/{varName3:.*?}/{varName4}

或者可能是

http://{varName1}/path1/path2/{varName2:\\w*}/{varName3:\\w*}/{varName4}

编辑

@RunWith(BlockJUnit4ClassRunner.class)
public class UriTemplateTest {

    private String URI = "http://hostname/path1/path2/design/99999/product/schema/75016TC806AA/TC806AA.tar";
    private String TEMPLATE_WORD = "http://{varName1}/path1/path2/{varName2:\\w*}/{varName3:\\w*}/{varName4}";
    private String TEMPLATE_RELUCTANT = "http://{varName1}/path1/path2/{varName2:.*?}/{varName3:.*?}/{varName4}";
    private Map<String, String> expected;

    @Before
    public void init() {
        expected = new HashMap<String, String>();
        expected.put("varName1", "hostname");
        expected.put("varName2", "design");
        expected.put("varName3", "99999");
        expected.put("varName4", "product/schema/75016TC806AA/TC806AA.tar");
    }

    @Test
    public void testTemplateWord() {
        testTemplate(TEMPLATE_WORD);
    }

    @Test 
    public void testTemplateReluctant() {
        testTemplate(TEMPLATE_RELUCTANT);
    }


    private void testTemplate(String template) {
        UriTemplate ut = new UriTemplate(template);
        Map<String, String> map = ut.match(URI);
        Assert.assertEquals(expected, map);
    }
}

【讨论】:

  • 我尝试了您的解决方案,但不幸的是没有成功。感谢您的建议。
  • 两种模式都可以正常工作。请发布您的测试代码和结果。
猜你喜欢
  • 2011-08-27
  • 2013-11-24
  • 2017-10-20
  • 1970-01-01
  • 2014-09-21
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多