当我在spring boot中使用杰克逊时,我得到了一个NoClassDefFoundError
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jacksonObjectMapper' defined in class path resource [org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$JacksonObjectMapperConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'jacksonObjectMapper' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.ObjectMapper
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1072)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:666)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:353)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:300)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1082)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1071)
at com.xxxx.Application.main(Application.java:22)
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)
......
......
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'jacksonObjectMapper' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.ObjectMapper
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 23 common frames omitted
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.ObjectMapper
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:597)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration.jacksonObjectMapper(JacksonAutoConfiguration.java:94)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$11271f08.CGLIB$jacksonObjectMapper$0(<generated>)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$11271f08$$FastClassBySpringCGLIB$$ce52140f.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration$$EnhancerBySpringCGLIB$$11271f08.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)
... 24 common frames omitted
然后我运行mvn dependency:tree -Dincludes=com.fasterxml.jackson.core
命令,然后得到:
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ xxxx-common ---
[INFO] com.xxxx.rc:xxxx-common:jar:1.0.0-SNAPSHOT
[INFO] \- com.fasterxml.jackson.core:jackson-databind:jar:2.11.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] \- com.fasterxml.jackson.core:jackson-core:jar:2.8.11:compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xxxx-dao 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ xxxx-dao ---
[INFO] com.xxxx.rc:xxxx-dao:jar:1.0.0-SNAPSHOT
[INFO] \- com.xxxx.rc:xxxx-common:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.fasterxml.jackson.core:jackson-databind:jar:2.11.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] \- com.fasterxml.jackson.core:jackson-core:jar:2.8.11:compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xxxx-service 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ xxxx-service ---
[INFO] com.xxxx.rc:xxxx-service:jar:1.0.0-SNAPSHOT
[INFO] \- com.xxxx.rc:xxxx-dao:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.xxxx.rc:xxxx-common:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.fasterxml.jackson.core:jackson-databind:jar:2.11.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] \- com.fasterxml.jackson.core:jackson-core:jar:2.8.11:compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xxxx-rpc 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ xxxx-rpc ---
[INFO] com.xxxx.rc:xxxx-rpc:jar:1.0.0-SNAPSHOT
[INFO] \- com.xxxx.rc:xxxx-service:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.xxxx.rc:xxxx-dao:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.xxxx.rc:xxxx-common:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.fasterxml.jackson.core:jackson-databind:jar:2.11.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] \- com.fasterxml.jackson.core:jackson-core:jar:2.8.11:compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xxxx-controller 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ xxxx-controller ---
[INFO] com.xxxx.rc:xxxx-controller:jar:1.0.0-SNAPSHOT
[INFO] \- com.xxxx.rc:xxxx-rpc:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.xxxx.rc:xxxx-service:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.xxxx.rc:xxxx-dao:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.xxxx.rc:xxxx-common:jar:1.0.0-SNAPSHOT:compile
[INFO] \- com.fasterxml.jackson.core:jackson-databind:jar:2.11.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] \- com.fasterxml.jackson.core:jackson-core:jar:2.8.11:compile
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] xxxx ................................. SUCCESS [ 0.736 s]
[INFO] xxxx-common .......................... SUCCESS [ 0.340 s]
[INFO] xxxx-dao ............................. SUCCESS [ 0.070 s]
[INFO] xxxx-service ......................... SUCCESS [ 0.284 s]
[INFO] xxxx-rpc ............................. SUCCESS [ 0.104 s]
[INFO] xxxx-controller ........................... SUCCESS [ 0.185 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
下面是我对json的用法。
public class JsonUtils {
private static ObjectMapper objectMapper;
static {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
}
public static String toString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new RuntimeException("parse object to json string error", e);
}
}
}
这是我的maven配置。
<dependencyManagement>
...
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
...
</dependencyManagement>
spring boot版本是1.5.22.RELEASE
。我不知道是什么导致了这个问题,谢谢你的帮助。
发布于 2021-10-21 07:51:34
我也面临着同样的问题,在添加了下面的依赖项后,它得到了解决。
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
https://stackoverflow.com/questions/64480067
复制相似问题