即使使用相同的版本,jackson也会抛出IllegalAccessError

时间:2017-03-01 07:36:29

标签: java maven jackson

我正在使用jackson开发一个软件,但完全卡住......当我使用java -jar <MY_APPLICATION>.jar命令运行spring-boot程序时,我收到以下错误:

...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'jacksonObjectMapper' threw exception; nested exception is
java.lang.IllegalAccessError: tried to access method com.fasterxml.jackson.databind.ser.std.StdSerializer.<init>(Ljava/lang/Class;)V from class com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        ... 84 more
Caused by: java.lang.IllegalAccessError: tried to access method com.fasterxml.jackson.databind.ser.std.StdSerializer.<init>(Ljava/lang/Class;)V from class com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
        at com.fasterxml.jackson.datatype.jsr310.JavaTimeModule.<init>(JavaTimeModule.java:159)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:78)
        at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.registerWellKnownModulesIfAvailable(Jackson2ObjectMapperBuilder.java:719)
        at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.configure(Jackson2ObjectMapperBuilder.java:572)
        at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:554)
        at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration.jacksonObjectMapper(JacksonAutoConfiguration.java:87)
        at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$2434a0bb.CGLIB$jacksonObjectMapper$0(<generated>)
        at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$2434a0bb$$FastClassBySpringCGLIB$$d685921f.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
        at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$2434a0bb.jacksonObjectMapper(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
        ... 85 more

错误之上还有更多错误消息,但我认为重要消息是java.lang.IllegalAccessError: tried to access method com.fasterxml.jackson.databind.ser.std.StdSerializer.<init>(Ljava/lang/Class;),我知道这种消息一般不会发生。

我确认jackson模块的版本相同(2.6.5来自spring-boot的依赖),并且不包括codehaus的jackson:

$ mvn dependency:tree -Dverbose -Dincluces=com.fasterxml.jackson.core | grep -v omitted

...

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ patriot-fdc-web ---
[INFO] <MY_GROUP>:<MY_APPLICATION_A>:war:0.3.9-SNAPSHOT
[INFO] +- <MY_GROUP>:<MY_APPLICATION_B>:jar:0.3.9-SNAPSHOT:compile
[INFO] |  \- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.6.5:compile
[INFO] |     +- com.fasterxml.jackson.core:jackson-core:jar:2.6.5:compile
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:1.3.3.RELEASE:compile
[INFO]    \- com.fasterxml.jackson.core:jackson-databind:jar:2.6.5:compile (version managed from 2.4.2)
[INFO]       +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.5:compile (version managed from 2.6.0)

有人会遇到这样的问题吗?任何人都可以建议任何解决方案或错误的可能性?提前谢谢。

4 个答案:

答案 0 :(得分:1)

我找到了原因。

依赖项包括calcite-avartica工件,并且它的版本为2.1.1,其中有一个带阴影的jackson。这就是我无法通过mvn dependency:tree找到依赖关系的原因。我通过查看StdSerializer的来源远程调试找到了它。 https://issues.apache.org/jira/browse/CALCITE-1110

Avartica 1.9.0或更高版本可能不会打扰我们。我刚刚排除了calcite-corecalcite-avartica,最后它运作良好。

答案 1 :(得分:1)

在sbt中有类似的问题,修复程序也是删除calcita-corecalcite-avatica之类的

ExclusionRule(organization = "org.apache.calcite", name = "calcite-core"),
ExclusionRule(organization = "org.apache.calcite", name = "calcite-avatica")

答案 2 :(得分:1)

我也遇到了类似的问题,这是由于curl -L http://localhost:8080 的传递依赖,而较低版本的jackson-datatype-jsr310也随之出现。通过在jackson-databind的第一级依赖项中添加exclude语句解决了该问题。

build.gradle

答案 3 :(得分:1)

如果您使用的是Maven,只需确定是哪个依赖项通过jsr310添加了Jackson的依赖项。为此,

  1. 在Eclispe中打开pom并选择选项卡依赖项层次结构并搜索jsr310

  2. 标识要添加依赖项的名称

  3. 将以下排除项添加到pom.xml

    <排除> <排除> com.fasterxml.jackson.datatype jackson-datatype-jsr310