本文介绍了URI模板需要与变量值匹配,该变量值是一组文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用org.springframework.web.util.UriTemplate,并且尝试匹配此uri模板:
I am using org.springframework.web.util.UriTemplate and I am trying to match this uri template:
http://{varName1}/path1/path2/{varName2}/{varName3}/{varName4}
具有以下uri:
http://hostname/path1/path2/design/99999/product/schema/75016TC806AA/TC806AA.tar
当前,我得到以下uri变量:
Currently I get the following uri variables:
{varName1=hostname, varName2=design/99999/product/schema, varName3=75016TC806AA,varName4=TC806AA.tar}
但是我想获得以下uri变量:
But I would like to get the following uri variables:
{varName1=hostname, varName2=design varName3=99999, varName4=product/schema/75016TC806AA/TC806AA.tar}
我尝试在模板中使用通配符作为*或+,但这似乎不起作用:
I tried to use wildcards as * or + in my template, but that doesn't seems to work:
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);
}
}
这篇关于URI模板需要与变量值匹配,该变量值是一组文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!