10

I'm trying to send a array / list of String to my REST server through Spring RestTemplate.

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

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]

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 RestTemplate but it does not actually answer the question.

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.

Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217

4 Answers4

18

Using Java 8, this worked for me :

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

It forms the URL like:

https://10.0.3.2:5000/getsubscribedarticles?articleids=123,456,789
userab
  • 503
  • 5
  • 10
15

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:

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!

Ralph
  • 118,862
  • 56
  • 287
  • 383
1

You did everything correct. You just need to call it without the [].

Just invoke it with .../getsubscribedarticles/articleids=foo,bar,42

I tested this with Spring Boot 1.2.6 and it works like this.

d0x
  • 11,040
  • 17
  • 69
  • 104
  • thanks for your answer - i did solve my problem in a similar way, see my answer below – Simon Nov 08 '15 at 13:05
  • I strongly believe that this is wrong. `articleids` is part of the query part of the url, and therefore `@RequestParam` should been used but not @PathVariable - see http://stackoverflow.com/questions/13715811/requestparam-vs-pathvariable - so in order to use `@PathVariable` it would been required also to modified the url so that the query parameter become an part of the path – Ralph Nov 08 '15 at 13:13
  • @Ralph You are right. I updated the answer and removed the part talking about PathVariables. – d0x Nov 08 '15 at 14:23
0

Thanks to dOx for his suggestion - I managed to solve this with the PathVariable - i set the list in my url for 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){

}
Simon
  • 19,658
  • 27
  • 149
  • 217