Spring 4.3.5和Jackson 2.8.7无法正常工作

时间:2018-04-02 13:03:01

标签: spring jackson

我正在使用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 and fasterxml Jackson 2.7.0 are incompatible的帮助,并将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 个答案:

答案 0 :(得分:1)

这绝对是版本兼容性问题,因此在运行时期间您拥有的jackson-databind版本的版本比您需要的版本早。 即使您已经更新了依赖关系,也许它会被其他东西覆盖;或者,也许你在classpath中有多个罐子(在这种情况下,一个是任意选择的......当然它通常是较旧的)。

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