栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Spring RestTemplate发送数组?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Spring RestTemplate发送数组?

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/实施。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596155.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号