Marco Bozzola
Marco Bozzola

Reputation: 189

RestTemplate.postForObject and Lists

Hi i want a client to consume a restfull webservice. I use springboot and the service return a JSON list with just ONE OBJECT. I want to use postForObject Api of RestTemplate like this

ResponseEntity<List<RetrieveRichiestaResponseDto>> result = restTemplate.postForObject(
                uri,
                entity,
                new ParameterizedTypeReference<List<RetrieveRichiestaResponseDto>>() {});

Why it gives me this error

> The method postForObject(String, Object, Class<T>, Object...) in the
> type RestTemplate is not applicable for the arguments (String,
> HttpEntity<capture#3-of ?>, new  
> ParameterizedTypeReference<List<RetrieveRichiestaResponseDto>>(){})

Thx for help!

Upvotes: 1

Views: 1042

Answers (1)

Eklavya
Eklavya

Reputation: 18430

restTemplate.postForObject() don't support ParameterizedTypeReference<>

Use restTemplate.exchange()

ResponseEntity<List<RetrieveRichiestaResponseDto>> result = restTemplate.exchange(uri, HttpMethod.POST, entity, 
       new ParameterizedTypeReference<List<RetrieveRichiestaResponseDto>>() {});

Upvotes: 2

Related Questions