RestTemplate fails to convert properly for Generic Type Container with MappingJacksonHttpMessageConverter [SPR-7002] #11667
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
Simon Wong opened SPR-7002 and commented
When I used RestTemplate to retrieve the result in Generic Type Container, the MappingJacksonHttpMessageConverter could not properly determine the type (due to erasure)
e.g.
List<MyDomain> myDomainList = restTemplate.getForObject(uri, List.class);
will throw ClassCastException
the MappingJacksonHttpMessageConverter could not determine that the List container should stores MyDomain objects by passing the List.class, it could only stores as List<LinkedHashedMap>
Recommend to add something similar to TypeReference in Jackson. http://wiki.fasterxml.com/JacksonInFiveMinutes (Section: Data Binding with Generics) to solve this issue.
Personal speaking, if the new HttpEntity could provide a setTypeReference() would be much convenient.
e.g.
HttpEntity httpEntity = new HttpEntity();
httpEntity.setTypeReference(new TypeReference<List<MyDomain>>() {});
List<MyDomain> myDomainList = restTemplate.getForObject(uri, httpEntity);
Affects: 3.0 GA, 3.0.1
Issue Links:
Referenced from: commits a1f6eab, 247c46e, 212daa1
0 votes, 7 watchers
The text was updated successfully, but these errors were encountered: