java Spring RestTemplate:在GET请求中发送字符串数组/列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33593390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring RestTemplate: sending array / list of String in GET request
提问by Simon
I'm trying to send a array / list of String to my REST server through Spring RestTemplate.
我正在尝试通过 Spring RestTemplate 将字符串数组/列表发送到我的 REST 服务器。
This is on my android side:
这是在我的安卓端:
private List<String> articleids = new ArrayList<>();
articleids.add("563e5aeb0eab252dd4368ab7");
articleids.add("563f2dbd9bb0152bb0ea058e");
final String url = "https://10.0.3.2:5000/getsubscribedarticles";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("articleids", articleids);
java.net.URI builtUrl = builder.build().encode().toUri();
Log.e("builtUrl", builtUrl.toString());
The builtUrl is: https://10.0.3.2:5000/getsubscribedarticles?articleids=%5B563e5aeb0eab252dd4368ab7,%20563f2dbd9bb0152bb0ea058e%5D
内置网址为:https://10.0.3.2:5000/getsubscribedarticles ?articleids =%5B563e5aeb0eab252dd4368ab7,%20563f2dbd9bb0152bb0ea058e%5D
On the server side:
在服务器端:
@RequestMapping(value = "/getsubscribedarticles", method = RequestMethod.GET)
public List<Posts> getSubscribedPostFeed(@RequestParam("articleids") List<String> articleids){
for (String articleid : articleids {
logger.info(" articleid : " + articleid);
}
}
The server logs:
服务器日志:
.13:11:35.370 [http-nio-8443-exec-5] INFO c.f.s.i.ServiceGatewayImpl - articleid : [563e5aeb0eab252dd4368ab7
.13:11:35.370 [http-nio-8443-exec-5] INFO c.f.s.i.ServiceGatewayImpl - articleid : 563f2dbd9bb0152bb0ea058e]
.13:11:35.370 [http-nio-8443-exec-5] 信息 cfsiServiceGatewayImpl - 文章 ID:[563e5aeb0eab252dd4368ab7
.13:11:35.370 [http-nio-8443-exec-5] 信息 cfsiServiceGatewayImpl - articleid:563f2dbd9bb0152bb0ea058e]
Which I can see is wrong as the list should not have a '[' on the first item and a ']' on the last item.
我可以看到这是错误的,因为列表的第一项不应有“[”,最后一项不应有“]”。
I have read this thread How to pass List or String array to getForObject with Spring RestTemplatebut it does not actually answer the question.
我已经阅读了这个线程How to pass List or String array to getForObject with Spring RestTemplate但它实际上并没有回答这个问题。
The selected answer issues out a POST request, but I want to do a GET request , also it requires an additional object to work to hold the list and I would prefer to not create extra objects if I can do it with Spring RestTemplate natively.
选定的答案发出一个 POST 请求,但我想做一个 GET 请求,它还需要一个额外的对象来保存列表,如果我可以在本地使用 Spring RestTemplate 来完成,我宁愿不创建额外的对象。
回答by Ralph
I would expect that the correct working url is something like:
我希望正确的工作网址是这样的:
https://10.0.3.2:5000/getsubscribedarticles?articleids[]=123&articleids[]=456&articleids[]=789
After a quick look at the code of public UriComponentsBuilder queryParam(String name, Object... values)
, I would solve it by using UriComponentsBuilder
this way:
快速浏览一下 的代码后public UriComponentsBuilder queryParam(String name, Object... values)
,我会使用UriComponentsBuilder
这种方式解决它:
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("articleids[]", articleids.toArray(new String[0]));
It is important that, the second parameter is an array
but not an Object/Collection!
重要的是,第二个参数是一个array
但不是一个对象/集合!
回答by userab
Using Java 8, this worked for me :
使用 Java 8,这对我有用:
UriComponentsBuilder builder = fromHttpUrl(url);
builder.queryParam("articleids", String.join(",", articleids));
URI uri = builder.build().encode().toUri();
It forms the URL like:
它形成如下 URL:
https://10.0.3.2:5000/getsubscribedarticles?articleids=123,456,789
回答by d0x
You did everything correct. You just need to call it without the []
.
你做的一切都是正确的。你只需要在没有[]
.
Just invoke it with .../getsubscribedarticles/articleids=foo,bar,42
只需调用它 .../getsubscribedarticles/articleids=foo,bar,42
I tested this with Spring Boot 1.2.6 and it works like this.
我用 Spring Boot 1.2.6 对此进行了测试,它的工作原理是这样的。
回答by Simon
Thanks to dOx for his suggestion - I managed to solve this with the PathVariable - i set the list in my url for android:
感谢 dOx 的建议 - 我设法用 PathVariable 解决了这个问题 - 我在我的 url 中为 android 设置了列表:
final String url = "https://10.0.3.2:5000/getsubscribedarticles/"+new ArrayList<>(articleids);
For my rest server:
对于我的休息服务器:
@RequestMapping(value = "/getsubscribedarticles/[{articleids}]", method = RequestMethod.GET)
public List<Posts> getSubscribedPostFeed(@PathVariable String[] articleids){
}