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

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

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

Comments

@spring-issuemaster
Copy link
Collaborator

commented Mar 17, 2010

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:

  • #11685 Proper handling of parameterized types in RestTemplate

Referenced from: commits a1f6eab, 247c46e, 212daa1

0 votes, 7 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 22, 2010

Arjen Poutsma commented

I'm not sure I understand the second part of the description. HttpEnity is not (designed to be) used as a argument for getForObject. HttpEntity either represents request/response entity that is to be posted/retrieved, it cannot be used in the above manner.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 22, 2010

Arjen Poutsma commented

Introducing the TypeReference concept is to big a change for a minor version. I've created #11685 (scheduled for 3.1) for that.

For the time being, I've introduced a simple workaround. There is now a getJavaType method, which you can override to customize the JavaType Jackson serializes to. For instance:

converter = new MappingJacksonHttpMessageConverter() {
	@Override
	protected JavaType getJavaType(Class<?> clazz) {
		if (List.class.isAssignableFrom(clazz)) {
			return TypeFactory.collectionType(ArrayList.class, MyBean.class);
		}
		else {
			return super.getJavaType(clazz);
		}
	}
};

This will have to do for now, before we properly tackle this in 3.1.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 23, 2010

Simon Wong commented

I guess I really misunderstand the response entity before (just treat it as the meta-data for the HTTP resources). It has been clarified after read the JIRA #11634 carefully.

Thanks for your help.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 8, 2010

Ioannis Foukarakis commented

A little bit late comment, but another workaround is to use
MyDomain[] myDomains = restTemplate.getForObject(uri, MyDomain[].class);

then convert the array to a list (if needed).
I had a similar issue, tested it with Spring 3.0.2 and it worked.

Also tried to use (new ArrayList<MyDomain>()).getClass(), but it returned ArrayList as a type and it failed (as expected).

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 8, 2010

Simon Wong commented

Thanks. But Array is reified (according to Java Language Spec 4.7), which will lost type safety

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.