Spring RestTemplate: sending array / list of String in GET request
Solution 1
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
Solution 2
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!
Solution 3
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.
Simon
I started as a really novice android developer. Like as in I just started reading a book. But I have learnt a lot now and what an experience it has been. Lately, I have already been really interested in using Zipline for Algorithmic Trading. So I started a blog on this topic, you can find it here: https://financialzipline.wordpress.com I have also developed a fully-fledged android app: https://play.google.com/store/apps/details?id=com.bekwaai&hl=en
Updated on July 09, 2022Comments
-
Simon almost 2 years
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.
-
Simon over 8 yearsthanks for your answer - i did solve my problem in a similar way, see my answer below
-
Ralph over 8 yearsI 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 stackoverflow.com/questions/13715811/… - 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 -
d0x over 8 years@Ralph You are right. I updated the answer and removed the part talking about PathVariables.