Spring的UriComponentsBuilder做到了这一点,并且还允许变量扩展。假设您要将字符串数组作为参数“
attr”传递给资源,而该资源只有一个带有路径变量的URI:
UriComponents comp = UriComponentsBuilder.fromHttpUrl( "http:/www.example.com/widgets/{widgetId}").queryParam("attr", "width", "height").build();UriComponents expanded = comp.expand(12);assertEquals("http:/www.example.com/widgets/12?attr=width&attr=height", expanded.toString());否则,如果您需要定义一个应该在运行时扩展的URI,并且您事先不知道数组的大小,请使用带有{?的http://tools.ietf.org/html/rfc6570
UriTemplate。 key *}占位符,并使用https://github.com/damnhandy/Handy-URI-
Templates中的UriTemplate类将其扩展
。
UriTemplate template = UriTemplate.fromTemplate( "http://example.com/widgets/{widgetId}{?attr*}");template.set("attr", Arrays.asList(1, 2, 3));String expanded = template.expand();assertEquals("http://example.com/widgets/?attr=1&attr=2&attr=3", expanded);对于Java以外的其他语言,请参见https://pre.google.com/p/uri-
templates/wiki/实施。



