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

Spring Boot整合Swagger出现NoClassDefFoundError错误求助

Spring Boot集成Swagger出现NoClassDefFoundError的原因及解决方法

错误信息

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup

原因分析

  • Spring Boot 2.7.x版本与SpringFox 3.0.0存在依赖兼容性冲突。org.springframework.core.metrics.ApplicationStartup是Spring Framework 5.3+新增的类,但SpringFox 3.0.0内部依赖的Spring版本和Spring Boot 2.7.4所依赖的版本不匹配,导致运行时无法找到该类。
  • SpringFox 3.0.0已停止维护,官方不再更新以适配高版本Spring Boot,这也是兼容性问题的核心原因。

解决方法

方案一:替换为SpringDoc(推荐)

SpringDoc是官方推荐的Swagger替代方案,完美兼容Spring Boot 2.7+,操作步骤如下:

  1. 移除pom.xml中所有SpringFox相关依赖:
<!-- 删除以下SpringFox依赖 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>
  1. 添加SpringDoc依赖:
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.14</version> <!-- 适配Spring Boot 2.7.x的稳定版本 -->
</dependency>
  1. 清理冗余配置:删除SwaggerConfiguration类,同时移除主类中的@Import({SwaggerConfiguration.class})注解。
  2. 启动项目后,访问http://localhost:8080/swagger-ui.html即可查看接口文档。

方案二:降级Spring Boot版本(不推荐,仅临时过渡)

若坚持使用SpringFox,可将Spring Boot版本降级到2.6.x及以下(如2.6.13),步骤如下:

  1. 修改pom.xml中spring-boot-starter-web的版本:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.6.13</version>
</dependency>
  1. 执行mvn clean install清理并重新构建依赖,启动项目即可恢复正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 18:56:01