Spring Boot微服务使用Jackson API时出现JSON转换错误求助
解决Jackson序列化时的
ClassNotFoundException: com.fasterxml.jackson.annotation.JsonKey问题 这个问题我之前排查过,核心原因和你POJO里的空字段完全无关,是Jackson依赖版本不兼容或者缺失了包含JsonKey类的核心注解包导致的:JsonKey是Jackson 2.12版本之后才引入的注解类,如果你的项目里混合了不同版本的Jackson核心模块(core、databind、annotations),或者没有正确引入jackson-annotations包,就会触发这个类找不到的错误。
下面是具体的解决步骤:
1. 统一Jackson核心模块的版本
确保jackson-core、jackson-databind、jackson-annotations这三个核心包的版本完全一致,建议选择一个稳定的新版本(比如2.15.2):
Maven配置示例
<dependencies> <!-- Jackson核心模块,版本必须统一 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.15.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.15.2</version> </dependency> </dependencies>
Gradle配置示例
dependencies { implementation 'com.fasterxml.jackson.core:jackson-core:2.15.2' implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2' implementation 'com.fasterxml.jackson.core:jackson-annotations:2.15.2' }
2. 排除依赖冲突
如果你的项目中其他第三方依赖间接引入了旧版本的Jackson,需要手动排除这些冲突的依赖,避免版本混乱:
Maven排除冲突示例
<dependency> <!-- 替换成你项目中引入的第三方依赖 --> <groupId>com.example</groupId> <artifactId>some-third-party-lib</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </exclusion> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </exclusion> </exclusions> </dependency>
3. 清理并重新构建项目
执行构建工具的清理命令,清除本地缓存的旧依赖,确保新的统一版本依赖被正确下载和使用:
- Maven:执行
mvn clean install - Gradle:执行
./gradlew clean build
完成以上步骤后,再运行你的序列化代码,这个类找不到的错误应该就能解决了。
内容的提问来源于stack exchange,提问作者Sakthi Velan
相关产品推荐
相关产品推荐

