13

In a test I wish to hit an endpoint which returns a list of a type. Currently I have

@Test
public void when_endpoint_is_hit_then_return_list(){
   //Given
   ParameterizedTypeReference<List<Example>> responseType = new ParameterizedTypeReference<List<Example>>() {};

   String targetUrl = "/path/to/hit/" + expectedExample.getParameterOfList();

   //When

   //This works however highlights an unchecked assignment of List to List<Example>
   List<Example> actualExample = restTemplate.getForObject(targetUrl, List.class);

   //This does not work
   List<Example> actualExample = restTemplate.getForObject(targetUrl, responseType);

   //This does not work either
   List<Example> actualExample = restTemplate.exchange(targetUrl, HttpMethod.GET, null, new ParameterizedTypeReference<List<Example>>() {});

   //Then
   //Assert Results
}

The problem for getForObject method is the ParameterizedTypeReference makes the getForObject method not resolve, as the types do not match up.

The problem for the exchange method is incompatible types. Required List but 'exchange' was inferred to ResponseEntity: no instance(s) of type variable(s) exist so that ResponseEntity conforms to List

How could I use the ParameterizedTypeReference correctly in this situation to safely return the List type I want?

1 Answer 1

22

From the documentation:

Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity. The given ParameterizedTypeReference is used to pass generic type information:

ParameterizedTypeReference<List<MyBean>> myBean =
   new ParameterizedTypeReference<List<MyBean>>() {};

ResponseEntity<List<MyBean>> response =
   template.exchange("http://example.com",HttpMethod.GET, null, myBean);

So in your case you can:

ResponseEntity<List<Example>> actualExample = restTemplate.exchange(targetUrl, HttpMethod.GET, null, new ParameterizedTypeReference<List<Example>>() {});
List<Example> exampleList = actualExample.getBody();
3
  • I literally just figured this about 5 minutes ago but ran into another problem where I get a : com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of Example (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator). My class does not and can not have a default constructor.
    – shirafuno
    Aug 17, 2018 at 15:43
  • 1
    See JsonCreator annotation for defining how to tell jackson to create an object without default constructor Aug 17, 2018 at 15:54
  • if data return null then this issue came no String-argument constructor/factory method to deserialize from String value ('could not execute statement;
    – Himanshu
    Jun 27, 2019 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.