Reputation: 11
I have created a POC to support gson instead of jackson with Spring MVC 5 and Java 8 but not using Spring Boot .
Reference tutorial
Even after removing jackson binding support from pom.xml and using gson, gives error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException
Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]:
Pom.xml entry :
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.4</version>
</dependency>
@Configuration public class CustomConfiguration {
@Bean
public AbstractJsonHttpMessageConverter customConverters() {
Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
messageConverters.add(gsonHttpMessageConverter);
return gsonHttpMessageConverter;
}
}
Dependency Graph
[INFO] +- com.oracle:ojdbc6:jar:11.2.0.3:compile
[INFO] +- com.google.code.gson:gson:jar:2.8.4:compile
[INFO] +- org.springframework:spring-core:jar:5.0.7.RELEASE:compile
[INFO] | \- org.springframework:spring-jcl:jar:5.0.7.RELEASE:compile
[INFO] +- org.hibernate:hibernate-core:jar:5.2.11.Final:compile
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] | +- org.javassist:javassist:jar:3.20.0-GA:compile
[INFO] | +- antlr:antlr:jar:2.7.7:compile
[INFO] | +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.1.Final:compile
[INFO] | +- org.jboss:jandex:jar:2.0.3.Final:compile
[INFO] | +- com.fasterxml:classmate:jar:1.3.0:compile
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile
[INFO] +- org.springframework:spring-web:jar:5.0.7.RELEASE:compile
[INFO] | \- org.springframework:spring-beans:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-tx:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:5.0.7.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:5.0.7.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:5.0.7.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:5.0.7.RELEASE:compile
[INFO] +- org.springframework:spring-test:jar:5.0.7.RELEASE:test
[INFO] +- org.springframework.data:spring-data-jpa:jar:2.0.8.RELEASE:compile
[INFO] | +- org.springframework.data:spring-data-commons:jar:2.0.8.RELEASE:compile
[INFO] | \- org.aspectj:aspectjrt:jar:1.8.13:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] +- log4j:log4j:jar:1.2.17:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.5:test
[INFO] +- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] +- org.hibernate:hibernate-validator:jar:4.3.2.Final:compile
[INFO] +- javax.validation:validation-api:jar:1.1.0.Final:provided
[INFO] +- org.apache.commons:commons-lang3:jar:3.7:compile
[INFO] \- junit:junit:jar:4.12:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test
Upvotes: 1
Views: 2150
Reputation: 11
Thanks Deepika for posting this question on our behalf. Responding to old thread, but here is the problem and how we resolved it.
Problem Even though we removed all Jackson dependencies from pom.xml and configured Spring to use GSON rather than Jackson for serialization and de-serialization, the container was trying to load some Jackson classes and as a result, throwing the above exception. The reason is that we were using JBoss-EAP-7. Jboss tries to load Resteasy module while application runtime (JAX-RS, JSR-311, is a new JCP specification that provides a Java API for RESTful Web Services over the HTTP protocol) When we look carefully in its module.xml (location: EAP-7.1.0\modules\system\layers\base\com\fasterxml\jackson\jaxrs\jackson-jaxrs-json-provider\main), we can see that it requires "com.fasterxml.jackson.core.jackson-databind" dependency. So, Jboss throws exception when it is unable to find this JAR in lib.
Solution 1: If you are not using Jboss's Resteasy implementation in your project, then just exclude that module from Jboss. Create file jboss-deployment-structure.xml within WEB-INF (for a JBoss WAR project, this location is same as that of jboss-web.xml).
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" />
</exclusions>
</deployment>
</jboss-deployment-structure>
Solution 2: Open the jackson-jaxrs-json-provider's module.xml (path mentioned above). And comment out the jackson-databind entry below:
<module xmlns="urn:jboss:module:1.5" name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider">
<resources>
<resource-root path="jackson-jaxrs-json-provider-2.8.9.redhat-1.jar"/>
<resource-root path="jackson-jaxrs-base-2.8.9.redhat-1.jar"/>
<resource-root path="jackson-module-jaxb-annotations-2.8.9.redhat-1.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.ws.rs.api"/>
<module name="javax.xml.bind.api"/>
<module name="com.fasterxml.jackson.core.jackson-annotations"/>
<module name="com.fasterxml.jackson.core.jackson-core"/>
<!--<module name="com.fasterxml.jackson.core.jackson-databind"/>-->
</dependencies>
</module>
Both the above solutions work perfectly (we have tested); they tell Spring container not to load the Jackson modules if Jackson is not present in classpath dependency.
Hope this helps others!
Upvotes: 1
Reputation: 1000
Probably this answer from reference given by @triplem earlier would help you. Its using Spring 5.0 not Spring Boot.
Upvotes: 1