Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper handling of parameterized types in RestTemplate [SPR-7023] #11685

Closed
spring-issuemaster opened this issue Mar 22, 2010 · 5 comments

Comments

@spring-issuemaster
Copy link
Collaborator

commented Mar 22, 2010

Arjen Poutsma opened SPR-7023 and commented

Currently, the RestTemplate has no way to handle generic types. In other words, the following does not work:

List<MyDomain> myDomainList = restTemplate.getForObject(uri, List.class);

This should be fixed.


Affects: 3.1.1

Issue Links:

  • #13482 RestTemplate Converting Generic Types ("is duplicated by")
  • #13028 HttpMessageConverter doesn't support typed collections
  • #19423 Add PATCH HTTP operation to RestTemplate
  • #11667 RestTemplate fails to convert properly for Generic Type Container with MappingJacksonHttpMessageConverter

Referenced from: commits ed3823b

14 votes, 16 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 18, 2010

Fotios Lioutsis commented

I am using version 3.0.3.RELEASE and found that the description above works (since #11667 is resolved). However there is no support for usage of RestTemplate getForObject with a Jackson JavaType instead. Passing responseType object as List.class does not use generics (like List<MyDomain> as response type argument in your example). There should be support as in ObjecMapper method:
public <T> T readValue(Reader src,JavaType valueType)
Perhaps the description should be updated with this in mind.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 18, 2010

Fotios Lioutsis commented

Clarification to the above comment: it is still failing with ClassCastException unless the workaround of #11667 is used.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Feb 1, 2012

Jay Xu commented

This scenario exposes the limitation of type-erasing of Java generic handling. Looking forward to solution

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented May 25, 2012

Mukta commented

Is a fix for this planned in any of the upcoming releases?
Is there a workaround in the time being? Thanks.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Aug 23, 2012

Rossen Stoyanchev commented

Commit ed3823b045fd19cdb801609eb034c93dd4d75c3f:

This change makes it possible to use the RestTemplate to read an HTTP
response into a target generic type object. The RestTemplate has three
new exchange() methods that accept ParameterizedTypeReference -- a
new class that enables capturing and passing generic type info.
See the Javadoc of the three new methods in RestOperations for a
short example.

To support this feature, the HttpMessageConverter is now extended by
GenericHttpMessageConverter, which adds a method for reading an
HttpInputMessage to a specific generic type. The new interface
is implemented by the MappingJacksonHttpMessageConverter and also by a
new Jaxb2CollectionHttpMessageConverter that can read read a generic
Collection where the generic type is a JAXB type annotated with
@XmlRootElement or @XmlType.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.