bsuart
bsuart

Reputation: 361

How to get List<Object> with RestTemplate(SpringBoot)

I want to get: List<User>. I have endpoint(GET) for getting users. It gets:

[
  {
    "id": "d71dcbca-54f3-4b19-aec4-3776bfe34730",
    "name": "test",
    "surname": "test",
    "login": "test",
    "password": "-26104458",
    "email": "test",
    "role": "user"
  }
]

I try get getting this list with using rest template:

ResponseEntity<User[]> responseEntity = rest.getForEntity(my-endpoint, User[].class);
        return Arrays.asList(responseEntity.getBody());

But I get this errror:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type

Question: How to get List<User> with using rest template?

Upvotes: 0

Views: 177

Answers (1)

C. Weber
C. Weber

Reputation: 937

Maybe you want to try this approach:

ResponseEntity<List<User>> responseEntity = rest.exchange(
                "your-endpoint",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<User>>() {
                });

See also https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#rest-resttemplate

Upvotes: 1

Related Questions