Skip to content

RestTemplate fails to convert properly for Generic Type Container with MappingJacksonHttpMessageConverter [SPR-7002] #11667

Closed
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions