提问者:小点点

Spring 4.3.5和Jackson 2.8.7无法正常工作


我正在使用springVersion='4.3.5。RELEASE'jacksonVersion='2.8.7'

但每次都抛出以下异常:

Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectWriter.forType(Lcom/fasterxml/jackson/databind/JavaType;)Lcom/fasterxml/jackson/databind/ObjectWriter;
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:278) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81) [spring-web-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:132) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) [spring-webmvc-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    ... 37 more

无法理解为什么它抛出此异常。从这个链接中得到了帮助Spring 4.2.3和fasterxml Jackson 2.7.0不兼容,并将jacksonVersion更改为兼容版本,正如专家所回答的那样,但我仍然遇到了同样的问题。

我的gradle文件如下所示:

springVersion           = '4.3.5.RELEASE'
springDataVersion       = '1.10.5.RELEASE'
jacksonVersion          ='2.8.7'

dependencies {

compile "org.springframework:spring-context:${springVersion}"
compile "org.springframework:spring-context-support:${springVersion}"
compile "org.springframework:spring-core:${springVersion}"
compile "org.springframework:spring-web:${springVersion}"
compile "org.springframework:spring-webmvc:${springVersion}"
compile "org.springframework:spring-tx:${springVersion}"
compile "org.springframework:spring-beans:${springVersion}"
compile "org.springframework:spring-aop:${springVersion}"
compile "org.springframework:spring-test:${springVersion}"
compile "org.springframework.data:spring-data-jpa:${springDataVersion}"
compile "org.springframework:spring-orm:${springVersion}"

compile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
compile "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
}

共1个答案

匿名用户

这绝对是一个版本兼容性问题,所以不知何故,你在运行时拥有的jackson-datind版本比你需要的要旧。即使你更新了依赖项,它也可能被其他东西覆盖;或者,你在类路径中有多个jar(在这种情况下,一个被任意选择…当然,它通常是旧的)。

我相当确定这是版本问题的原因是,它是JVM链接器发现问题:正在运行的版本是针对具有该方法的较新版本编译的,但正在加载的版本没有该方法。