Search code examples
javaspring-bootrestweb-servicesresttemplate

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!


Solution

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

    Use restTemplate.exchange()

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