【发布时间】:2016-02-09 04:04:57
【问题描述】:
我正在尝试通过 Spring RestTemplate 将字符串数组/列表发送到我的 REST 服务器。
这是在我的安卓端:
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());
在服务器端:
@RequestMapping(value = "/getsubscribedarticles", method = RequestMethod.GET)
public List<Posts> getSubscribedPostFeed(@RequestParam("articleids") List<String> articleids){
for (String articleid : articleids {
logger.info(" articleid : " + articleid);
}
}
服务器日志:
.13:11:35.370 [http-nio-8443-exec-5] 信息 c.f.s.i.ServiceGatewayImpl - 文章编号:[563e5aeb0eab252dd4368ab7
.13:11:35.370 [http-nio-8443-exec-5] 信息 c.f.s.i.ServiceGatewayImpl - 文章编号:563f2dbd9bb0152bb0ea058e]
我认为这是错误的,因为列表不应该在第一项上包含“[”,在最后一项上不应该包含“]”。
我已经阅读了这个帖子How to pass List or String array to getForObject with Spring RestTemplate,但它实际上并没有回答这个问题。
选择的答案发出一个 POST 请求,但我想做一个 GET 请求,它还需要一个额外的对象来保存列表,如果我可以用 Spring RestTemplate 来做,我宁愿不创建额外的对象本机。
【问题讨论】:
标签: java android arrays spring resttemplate