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

Improve Jackson 2.7 compatibility [SPR-13853] #18426

Closed
spring-projects-issues opened this issue Jan 9, 2016 · 12 comments
Closed

Improve Jackson 2.7 compatibility [SPR-13853] #18426

spring-projects-issues opened this issue Jan 9, 2016 · 12 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jan 9, 2016

Marek Wiącek opened SPR-13853 and commented

RestTemplate produces the following error when used with Jackson 2.7.0-rc3:

Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.type.TypeFactory.constructType(Ljava/lang/reflect/Type;Ljava/lang/Class;)Lcom/fasterxml/jackson/databind/JavaType;
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.getJavaType(AbstractJackson2HttpMessageConverter.java:314)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.canRead(AbstractJackson2HttpMessageConverter.java:146)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.canRead(AbstractJackson2HttpMessageConverter.java:141)
at org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback.doWithRequest(RestTemplate.java:706)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:594)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:289)


Affects: 4.2.3, 4.2.4

Issue Links:

Referenced from: commits 09db26a

1 votes, 9 watchers

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 9, 2016

Juergen Hoeller commented

We are aware of this already but unfortunately can't fix this easily since there is no immediate replacement for that particular TypeFactory method, and in particular no known option that works on both Jackson 2.6 and 2.7 at least. #17869 tracks our effort to address this for 4.3.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

To be clear, this means that the current Spring Framework 4.2.x generation is likely to remain incompatible with Jackson 2.7+ and will rather just keep supporting Jackson 2.1 to 2.6. As of Spring Framework 4.3, we intend to raise this to Jackson 2.6+, with first-class support for Jackson 2.7 and the upcoming 2.8... and avoiding the use of deprecated Jackson API etc so the chances of working with future Jackson versions are as high as possible. Spring Framework 4.3.x will the retain that Jackson 2.6+ baseline for its entire extended support life.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Abhijit Sarkar commented

@Juergen Hoeller, when can we expect 4.3 to be released? I see a SNAPSHOT out there.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

You can always check https://jira.spring.io/browse/SPR/?selectedTab=com.atlassian.jira.jira-projects-plugin:roadmap-panel for the current dates. At this point, 4.3 RC1 is scheduled for mid March, with GA expected for early May.

Is there a specific need for Jackson 2.7 on your side, beyond what Jackson 2.6.4 can do for you?

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Abhijit Sarkar commented

Is there a specific need for Jackson 2.7 on your side

We were on Jackson 2.5 and needed this -
mapper.enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)

After the issue with Jackson 2.7, I found out that 2.6.4 also has this attribute so I can live with that. However, as a general rule, I try not to write software that only works with a certain dependency version. I'll wait until the fix comes out to upgrade Spring as well as Jackson.

Thanks for your response.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Feb 4, 2016

Elad Tabak commented

I'm facing a similar issue, using spring 4.2.4 (latest) and jackson 2.7.1 (also latest).
Only I'm getting this exception:

java.lang.IllegalArgumentException: Unrecognized Type: [null]
	com.fasterxml.jackson.databind.type.TypeFactory._fromAny(TypeFactory.java:1109)
	com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:566)
	com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:602)
	org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.getJavaType(AbstractJackson2HttpMessageConverter.java:311)
	org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:249)
	org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)

I was able to identify that the AbstractJackson2HttpMessageConverter.getJavaType() method on line 249 sends the second argument as null, instead of providing the class of the object/value as needed. Using a debugger I managed to change the null with the object class, and it worked.

This seems to be an easy fix for future versions of spring, to support jackson 2.7.1+.
Any chance to fix this in future versions?

It also relates to this bug that does not seem to be resolved in spring 4.2.4, as the second argument is still null:
#17903

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Feb 4, 2016

Juergen Hoeller commented

Indeed, Jackson 2.6's constructType(Type, Class) variant accepts a null value for the latter argument, but Jackson 2.7.1's reintroduced method of that signature insists on the second argument to be non-null. I guess we could simply call constructType(Type) if the context class is null which would restore Jackson 2.6 behavior. Let's try that for 4.2.5.

At the same time, 4.3 features a refactored approach where this problem doesn't appear anymore: see #18301. This needs to go through the 4.3 RC phase yet though.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

This commit introduces the conditional call proposed by Juergen Hoeller in AbstractJackson2HttpMessageConverter#getJavaType() in order to avoid calling TypeFactory#constructType() with a null contextClass parameter, since this is not supported by Jackson 2.7 anymore.

It allows to use Spring Framework 4.2.x with Jackson 2.7.1+ for most use cases, but with some limitations. For example, with Jackson 2.7 TypeVariable resolution from method parameters does not work.

As a consequence, after this commit Spring Framework 4.2.x still fully supports Jackson up to version 2.6, but improves Jackson 2.7 compatibility. Full support for Jackson 2.7 is provided as of Spring Framework 4.3.x.

@spring-projects-issues
Copy link
Collaborator Author

Elad Tabak commented

Sébastien thank you very much for the quick fix!

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

Juergen Hoeller I just found this Jackson issue that shows that Jackson 2.7.2 will work fine with null contextClass, so I am wondering if we should not revert the commit I pushed (except the Javadoc changes). Any thoughts?

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I guess since that code is just more defensive but otherwise functionally equivalent, there's nothing wrong with keeping it either?

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

Indeed, and since we are not sure when Jackson 2.7.2 will be released, it still can help users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants