异常:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode
at org.apache.kafka.clients.admin.NewTopic.convertToCreatableTopic(NewTopic.java:127)
at org.apache.kafka.clients.admin.KafkaAdminClient.createTopics(KafkaAdminClient.java:1422)
at org.apache.kafka.clients.admin.Admin.createTopics(Admin.java:127)
at org.springframework.kafka.core.KafkaAdmin.addTopics(KafkaAdmin.java:264)
at org.springframework.kafka.core.KafkaAdmin.addTopicsIfNeeded(KafkaAdmin.java:221)
at org.springframework.kafka.core.KafkaAdmin.initialize(KafkaAdmin.java:189)
at org.springframework.kafka.core.KafkaAdmin.afterSingletonsInstantiated(KafkaAdmin.java:157)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:963)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:123)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
问题出现:
@Bean public NewTopic topic() { return TopicBuilder.name("custom-kafka-1") .partitions(10) .replicas(3) .compact() .build(); }
原因:
Jackson包版本冲突导致
项目Springboot:2.4.2
引入spring-kafka版本:2.6.5
spring-kafka依赖的Jackson版本为 2.11.3
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.3</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.3</version> <scope>compile</scope> <optional>true</optional> </dependency>
springboot自带依赖的Jackson版本:2.11.4
导致 spring-kafka 依赖的 Jackson 2.11.3版本失效,实际依赖的是2.11.4版本。
解决方案:
在项目中添加Jackson依赖,覆盖springboot和spring-kafka的Jackson依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.11.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.3</version> </dependency>
END.
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode
原文:https://www.cnblogs.com/yangyongjie/p/14401960.html