Return the List<myObj> returned by ResponseEntity<List>
Asked Answered
D

6

19

My REST client uses RestTemplate to obtain a List of objects.

ResponseEntitiy<List> res = restTemplate.postForEntity(getUrl(), myDTO, List.class);

Now I want to use the list returned and return it as List to the calling class. In case of string, toString could be used, but what is the work around for lists?

Danseuse answered 18/8, 2014 at 1:25 Comment(0)
G
33

First off, if you know the type of elements in your List, you may want to use the ParameterizedTypeReference class like so.

ResponseEntity<List<MyObj>> res = restTemplate.postForEntity(getUrl(), myDTO, new ParameterizedTypeReference<List<MyObj>>() {});

Then if you just want to return the list you can do:

return res.getBody();

And if all you care about is the list, you can just do:

// postForEntity returns a ResponseEntity, postForObject returns the body directly.
return restTemplate.postForObject(getUrl(), myDTO, new ParameterizedTypeReference<List<MyObj>>() {});
Godmother answered 18/8, 2014 at 5:48 Comment(0)
C
18

I couldn't get the accepted answer to work. It seems postForEntity no longer has this method signature. I had to use restTemplate.exchange() instead:

ResponseEntity<List<MyObj>> res = restTemplate.exchange(getUrl(), HttpMethod.POST, myDTO, new ParameterizedTypeReference<List<MyObj>>() {});

Then to return the list, as above:

return res.getBody();
Compander answered 13/12, 2017 at 9:12 Comment(1)
I confirmed postFor entity is no longer available. Exchange method worksHackery
C
10

In the latest version (Spring Framework 5.1.6) both the answers are not working. As kaybee99 mentioned in his answer postForEntity method signature got changed. Also the restTemplate.exchange() method and its overloads need a RequestEntity<T> or its parent HttpEntity<T> object. Unable to pass my DTO object as mentioned.

Checkout the documentation of the RestTemplate class

Here is the code which worked for me

List<Shinobi> shinobis = new ArrayList<>();
shinobis.add(new Shinobi(1, "Naruto", "Uzumaki"));
shinobis.add(new Shinobi(2, "Sasuke", "Uchiha");
RequestEntity<List<Shinobi>> request = RequestEntity
            .post(new URI(getUrl()))
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .body(shinobis);
ResponseEntity<List<Shinobi>> response = restTemplate.exchange(
            getUrl(), 
            HttpMethod.POST, 
            request, 
            new ParameterizedTypeReference<List<Shinobi>>() {}
            );
List<Shinobi> result = response.getBody();

Hope it helps someone.

Candace answered 1/5, 2019 at 19:8 Comment(0)
L
3

You can use the ParameterizedTypeReference class of Spring to convert to List the data returned by ResponseEntity

ResponseEntity<List<MyObject>>  resp = restTemplate.exchange(url, HttpMethod.GET, null, new ParameterizedTypeReference<List<MyObject>>(){});

if(resp != null && resp.hasBody()){
   List<MyObject> myList = resp.getBody();
}
Lewanna answered 18/5, 2020 at 23:7 Comment(0)
B
0

You have unwrap the ResponseEntity and you can get the object(list)

 res.getBody()
Benavidez answered 4/10, 2019 at 11:46 Comment(0)
A
0

From here: https://www.baeldung.com/spring-rest-template-list

ResponseEntity<Team[]> response = this.restTemplate.getForEntity(
    this.API_URL(), Team[].class);
Africa answered 7/6, 2023 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.