restspring-mvcspring-bootresttemplaterestful-architecture

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());

Solution

  • Try use ParameterizedTypeReference

    ResponseEntity<List<Menu>> response = restTemplate
           .exchange("URI", HttpMethod.GET, entity,  new ParameterizedTypeReference<List<Menu>>() {
     });