Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestTemplate: sending array / list of String in GET request

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.

like image 820
Simon Avatar asked Nov 08 '15 11:11

Simon


People also ask

How do I pass a Multipartfile in RestTemplate?

Set the content-type header value to MediaType. MULTIPART_FORM_DATA. When this header is set, RestTemplate automatically marshals the file data along with some metadata. Next, build the request body as an instance of LinkedMultiValueMap class.

Is RestTemplate getting deprecated?

RestTemplate will still be used. But in some cases, the non-blocking approach uses much fewer system resources compared to the blocking one.

What is the correct way of calling a REST API using RestTemplate that returns a list of long values?

correct way is , @Autowired RestTemplate class and use that Instead of create new object every time..


2 Answers

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
like image 56
userab Avatar answered Nov 02 '22 21:11

userab


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!

like image 25
Ralph Avatar answered Nov 02 '22 22:11

Ralph