11

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.

4 Answers 4

21

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
Sign up to request clarification or add additional context in comments.

Comments

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!

Comments

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.

3 Comments

thanks for your answer - i did solve my problem in a similar way, see my answer below
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 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
@Ralph You are right. I updated the answer and removed the part talking about PathVariables.
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){

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.