You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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。

  • 解决方案步骤:

    1. 统一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'
      
    2. 排查依赖冲突
      用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'
      }
      
    3. 使用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组件的兼容版本,彻底避免版本冲突。

    4. 清理缓存并重建
      执行命令清理旧依赖缓存,重新构建项目:

      ./gradlew clean build --refresh-dependencies
      

内容的提问来源于stack exchange,提问作者plutownium

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:31:11