Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestTemplate: returning a List of Entities

There is a RestFull method that return a List of Menu objects

public ResponseEntity<List<Menu>> getMenus() {
..
}

But I don't know how to get them from the RestTemplate, getting the class from ResponseEntity>

ResponseEntity<List<Menu>> response = restTemplate
                  .exchange("http://127.0.0.1:8080/elcor/api/users/1/menus", HttpMethod.GET, entity,  ResponseEntity<List<Menu>>.getClass());
like image 527
en Peris Avatar asked May 26 '18 08:05

en Peris


People also ask

How do I extract a list of objects from ResponseEntity?

setContentType(MediaType. APPLICATION_JSON); Employee employee = new Employee(); HttpEntity<Employee> entity = new HttpEntity<Employee>(employee, headers); ResponseEntity<String> result = restTemplate. exchange(uri, HttpMethod. GET, entity, String.

Why RestTemplate is deprecated?

RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof. First, we create a Spring Boot project with the spring-boot-starter-web dependency.

What is difference between getForObject and getForEntity?

For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.


1 Answers

Try use ParameterizedTypeReference

ResponseEntity<List<Menu>> response = restTemplate
       .exchange("URI", HttpMethod.GET, entity,  new ParameterizedTypeReference<List<Menu>>() {
 });
like image 158
Hadi J Avatar answered Sep 30 '22 17:09

Hadi J