2

We are using restemplate in a commons package for our application. So we need to use generic types.

I read many solutions about that, but none seems to not work for us, and we constantly get (on the client side):

java.util.LinkedHashMap cannot be cast to nc.gouv.dsf.ranch.model.Pays

Here is the code (sum up):

public List<T> findAll(C criteria) {
[...]
                ResponseEntity<List<T>> response = 
                    restTemplateFactory.getRestTemplate().exchange(
                            url, 
                            HttpMethod.GET, 
                    new HttpEntity<>(createHttpHeaders(srvId)),
                    new ParameterizedTypeReference<List<T>>() {}
                            );          

            return response.getBody();
}

I though ParameterizedTypeReference what the solution to this kind of problems but its doesn't work.

PS: we are using springboot 1.3.1.RELEASE

Tyvain
  • 2,640
  • 6
  • 36
  • 70
  • Having the same problem and having worked with a number of different testing frameworks, I can assure you that Spring Boot + Hibernate == a testing nightmare. [Object-Relational Mapping is the Vietnam of Computer Science](https://blog.codinghorror.com/object-relational-mapping-is-the-vietnam-of-computer-science/) – Alex Barker Jul 01 '18 at 19:28

1 Answers1

0

It seems the request is returning a Map and we are trying to cast it into list, hence the exception. Can you try the following:

ResponseEntity<Map<String, Object>> response = 
            restTemplateFactory.getRestTemplate().exchange(
                    url, 
                    HttpMethod.GET, 
            new HttpEntity<>(createHttpHeaders(srvId)),
            new ParameterizedTypeReference<Map<String, Object>>() {}
                    );
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Not working either: "org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token at [Source: java.io.ByteArrayInputStream@1e8d552; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token" And also I loose the advantage of generic type with Object. – Tyvain Mar 07 '16 at 04:41
  • Can you add the example response in question? – Darshan Mehta Mar 07 '16 at 09:20
  • Please add example JSON payloads and bit more data into the question. – Michal Foksa Mar 07 '16 at 11:08