簡體   English   中英

在Spring MVC Application(不使用spring boot)中用gson替換了jackson(但沒有使用jackson依賴項時出錯)

[英]replaced jackson with gson in spring MVC Application( not using spring boot ) but still getting error for jackson dependency

我已經創建了一個POC以在Spring MVC 5和Java 8中支持gson而不是jackson,但是不使用Spring Boot。
參考教程

即使從pom.xml中刪除了傑克遜綁定支持並使用gson,也會產生錯誤:

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條目:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.4</version>
</dependency>

@Configuration公共類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

感謝Deepika代表我們發布此問題。 響應舊線程,但這是問題以及我們如何解決它。

問題即使我們從pom.xml中刪除了所有Jackson依賴關系,並將Spring配置為使用GSON而不是Jackson進行序列化和反序列化,該容器仍試圖加載某些Jackson類,結果拋出了上述異常。 原因是我們正在使用JBoss-EAP-7 Jboss嘗試在應用程序運行時加載Resteasy模塊(JAX-RS,JSR-311是一個新的JCP規范,它通過HTTP協議提供了用於RESTful Web服務的Java API)當我們仔細查看其module.xml(位置:EAP)時-7.1.0 \\ modules \\ system \\ layers \\ base \\ com \\ fasterxml \\ jackson \\ jaxrs \\ jackson-jaxrs-json-provider \\ main),我們可以看到它需要“ com.fasterxml.jackson.core.jackson-databind ”依賴項。 因此,當無法在lib中找到該JAR時,Jboss將引發異常。

解決方案1:如果您不在項目中使用Jboss的Resteasy實現,則只需將該模塊從Jboss中排除即可。 在WEB-INF中創建文件jboss-deployment-structure.xml(對於JBoss WAR項目,此位置與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>

解決方案2:打開jackson-jaxrs-json-provider的module.xml(上述路徑)。 並注釋掉下面的jackson-databind條目:

<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>

以上兩種解決方案都可以完美運行(我們已經測試過); 他們告訴Spring容器,如果classpath依賴項中不存在Jackson,則不要加載Jackson模塊。

希望這對別人有幫助!

@triplem先前提供的參考中的答案可能會幫助您。 它使用Spring 5.0而不是Spring Boot。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM