gpt4 book ai didi

java - URI 模板需要与作为一组文件夹的变量值匹配

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:06 27 4
gpt4 key购买 nike

我正在使用 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}

最佳答案

尝试:

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

关于java - URI 模板需要与作为一组文件夹的变量值匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16671292/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com