Spring Boot测试报错:Failed to load ApplicationContext求助
解决Spring Boot测试中
ClassNotFoundException: com.fasterxml.jackson.databind.cfg.EnumFeature问题 核心原因:
EnumFeature是Jackson 2.13版本才新增的配置类,你当前使用的jackson-databind:2.12.5根本不包含这个类。要么是项目中其他依赖间接引入了高版本Jackson组件,导致版本冲突;要么是代码/框架自动配置用到了仅在高版本Jackson中存在的API。解决方案步骤:
统一Jackson版本
直接升级jackson-databind到2.13.x及以上版本,同时保证jackson-core、jackson-annotations和jackson-databind三者版本一致:testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.13.5' testImplementation 'com.fasterxml.jackson.core:jackson-core:2.13.5' testImplementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.5'排查依赖冲突
用Gradle命令生成依赖树,找出引入不兼容Jackson版本的依赖:./gradlew dependencies --configuration testRuntimeClasspath搜索输出里的
com.fasterxml.jackson相关条目,若发现不同版本组件,可通过强制统一版本解决:configurations.all { resolutionStrategy.force 'com.fasterxml.jackson.core:jackson-databind:2.13.5' resolutionStrategy.force 'com.fasterxml.jackson.core:jackson-core:2.13.5' resolutionStrategy.force 'com.fasterxml.jackson.core:jackson-annotations:2.13.5' }使用Spring Boot管理的依赖版本(推荐)
如果是Spring Boot项目,优先依赖Spring Boot starter,避免手动指定Jackson版本。移除手动添加的Jackson依赖,确保引入相关starter:dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' }Spring Boot会自动管理所有Jackson组件的兼容版本,彻底避免版本冲突。
清理缓存并重建
执行命令清理旧依赖缓存,重新构建项目:./gradlew clean build --refresh-dependencies
内容的提问来源于stack exchange,提问作者plutownium
相关产品推荐
相关产品推荐

