java - 在 spring MVC 应用程序中将 jackson 替换为 gson (不使用 spring boot ),但仍然出现 jackson 依赖错误

标签 java spring-mvc jackson gson

我使用 Spring MVC 5 和 Java 8 创建了一个 POC 来支持 gson 而不是 jackson,但不使用 Spring Boot。
Reference tutorial

即使从 pom.xml 中删除了 jackson 绑定(bind)支持并使用 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>

@配置 公共(public)类自定义配置{

@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

最佳答案

可能this answer来自 @triplem 之前提供的引用会对您有所帮助。它使用 Spring 5.0 而不是 Spring Boot。

关于java - 在 spring MVC 应用程序中将 jackson 替换为 gson (不使用 spring boot ),但仍然出现 jackson 依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52496570/

相关文章:

maven - 即使添加依赖项后,Intellij IDEA jSTL 和 taglib 也无法识别

java - 使用 Google Http Java Client 将嵌套的 JSON 节点解析为 POJO

java - 如何使用 Jackson 将 HashMap 转换为 JsonNode?

java - Hibernate,在计数中使用具有多个列的不同

java - 无法在 junit 测试中从应用程序上下文加载 bean

java - 如何从 spring mvc Controller 调用外部 api

Java 获取封闭泛型类型的类常量(Class<T1<T2>> 的实例)

java - JFileChooser 关闭启动新窗口

java - 列出 c2dm/GCM 注册应用程序

java - 如何去除应用栏上方的阴影?