Hi I have opened the question on stack overflow (http://stackoverflow.com/questions/40786366/force-spring-boot-to-use-gson-instead-of-jackson) but nobody is able to answer me until now :/ For me it seems like a bug...
Spring-Boot 1.4.2 reference claims:
spring.http.converters.preferred-json-mapper=jackson # Preferred JSON mapper to use for HTTP message conversion. Set to "gson" to force the use of Gson
- We have set gson for preferred-json-mapper.
- We have added Gson as dependency of our project.
But still Jackson was used.
During debug I have found:
public AllEncompassingFormHttpMessageConverter() {
addPartConverter(new SourceHttpMessageConverter<Source>());
if (jaxb2Present && !jackson2XmlPresent) {
addPartConverter(new Jaxb2RootElementHttpMessageConverter());
}
if (jackson2Present) {
addPartConverter(new MappingJackson2HttpMessageConverter());
}
else if (gsonPresent) {
addPartConverter(new GsonHttpMessageConverter());
}
if (jackson2XmlPresent) {
addPartConverter(new MappingJackson2XmlHttpMessageConverter());
}
}
Based on above seems that Gson will be not used every time when Jackson will be on classpath...
So finally we manage to force Spring-Boot to use Gson after exclude all transitive dependencies in Maven which have been pointing out to Jackson.
Now the question is. Is it the only way to force Spring-Boot to use Gson instead of Jackson? Do we really need to exclude all transitive dependencies pointing out to Jackson? Shouldn't preferred-json-mapper setting be enough?
Comment From: wilkinsona
How have you identified that the Jackson-based converter is still being used? While AllEncompassingFormHttpMessageConverter prefers Jackson over GSON, Spring Boot's HTTP message converter configuration updates its part converters so that the GSON converter will take priority. It ends up configured with the following converters:
- org.springframework.http.converter.json.GsonHttpMessageConverter
- org.springframework.http.converter.ByteArrayHttpMessageConverter
- org.springframework.http.converter.StringHttpMessageConverter
- org.springframework.http.converter.StringHttpMessageConverter
- org.springframework.http.converter.ResourceHttpMessageConverter
- org.springframework.http.converter.xml.SourceHttpMessageConverter
- org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter
- org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
Note that GsonHttpMessageConverter appears first in the list to it will be used in preference to MappingJackson2HttpMessageConverter at the end of the list.
Comment From: wilkinsona
Here's the question on Stack Overflow: https://github.com/spring-projects/spring-boot/issues/7518
@Ziemowit I suspect that no one answered it as you haven't provided enough information. You've said that something isn't working as expected, but haven't provided an example that reproduces it. If you want people to spend time helping you, then spending some time producing a minimal, complete, verifiable example will make it much easier for them to do so.
Comment From: Ziemowit
Hmmm ok I will try to prepare some minimal example!
Comment From: Ziemowit
Ok I have prepared the example and it... works... (palm face) Sorry that I did not it before. My current application is much bigger and much more complicated. I just can't send it as an example. Slowly we move it to Spring Boot. So seems that bug is somewhere in our application / set up. I need to find out then why on my simple example just works but it does not in our big, real application.
Thank you for your help and sorry for wasting your time.
//--------- Root Cause
It occurred that in part of our old *.xml config which was used by Spring Boot we had
It was causing creation of RequestMappingHandlerAdapter 2nd time with default converters without GsonHttpMessageConverter.