【问题标题】:Spring RestTemplate: sending array / list of String in GET requestSpring RestTemplate:在 GET 请求中发送数组/字符串列表
【发布时间】: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());

builtUrl 为:https://10.0.3.2:5000/getsubscribedarticles?articleids=%5B563e5aeb0eab252dd4368ab7,%20563f2dbd9bb0152bb0ea058e%5D

在服务器端:

 @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


    【解决方案1】:

    使用 Java 8,这对我有用:

    UriComponentsBuilder builder = fromHttpUrl(url);
    builder.queryParam("articleids", String.join(",", articleids));
    URI uri = builder.build().encode().toUri();
    

    它形成如下 URL:

    https://10.0.3.2:5000/getsubscribedarticles?articleids=123,456,789
    

    【讨论】:

      【解决方案2】:

      我希望正确的工作网址类似于:

      https://10.0.3.2:5000/getsubscribedarticles?articleids[]=123&articleids[]=456&articleids[]=789
      

      快速查看public UriComponentsBuilder queryParam(String name, Object... values)的代码后,我会这样使用UriComponentsBuilder来解决:

      UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
          .queryParam("articleids[]", articleids.toArray(new String[0]));
      

      重要的是,第二个参数是array,而不是对象/集合!

      【讨论】:

        【解决方案3】:

        你做的一切都是正确的。你只需要在没有[] 的情况下调用它。

        只需使用.../getsubscribedarticles/articleids=foo,bar,42 调用它

        我用 Spring Boot 1.2.6 对此进行了测试,它的工作原理是这样的。

        【讨论】:

        • 感谢您的回答 - 我确实以类似的方式解决了我的问题,请参阅下面的回答
        • 我坚信这是错误的。 articleids 是 url 查询部分的一部分,因此应该使用 @RequestParam 而不是 @PathVariable - 请参阅 stackoverflow.com/questions/13715811/… - 因此,为了使用 @PathVariable,还需要修改 url,以便查询参数成为路径的一部分
        • @Ralph 你是对的。我更新了答案并删除了谈论 PathVariables 的部分。
        【解决方案4】:

        感谢 dOx 的建议 - 我设法用 PathVariable 解决了这个问题 - 我在我的 url 中为 android 设置了列表:

            final String url = "https://10.0.3.2:5000/getsubscribedarticles/"+new ArrayList<>(articleids);
        

        对于我的休息服务器:

                @RequestMapping(value = "/getsubscribedarticles/[{articleids}]", method = RequestMethod.GET)
        public List<Posts> getSubscribedPostFeed(@PathVariable String[] articleids){
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多