SpringBoot 3.0.6集成Swagger v3.0.0加载API定义失败求助
问题解决:SpringBoot3.x + Swagger API文档加载失败(500错误)
错误根源
你同时引入了springfox-boot-starter(3.0.0)和springdoc-openapi-starter-webmvc-ui(2.0.3),这两个都是实现OpenAPI规范的框架,但底层依赖的swagger核心库版本不兼容:
- SpringFox 3.0.0适配SpringBoot 2.x,依赖的swagger-models版本里,
Schema类不存在getExampleSetFlag()方法; - SpringDoc 2.0.3适配SpringBoot 3.x,依赖的swagger-models版本新增了这个方法。
两者的依赖冲突导致类加载时抛出NoSuchMethodError,直接引发/v3/api-docs接口返回500状态码。
解决步骤
移除冲突依赖
直接删除pom.xml中的springfox-boot-starter依赖,SpringBoot 3.x官方推荐使用SpringDoc作为OpenAPI文档工具,SpringFox对SpringBoot 3.x的支持不完善。清理依赖缓存
执行Maven命令清理本地缓存,避免残留的冲突依赖:mvn clean install -U清理冗余配置
删除项目中所有SpringFox相关的配置类(比如带@EnableSwagger2注解的类),只保留SpringDoc的配置(如果有的话)。重启服务验证
重启SpringBoot服务后,访问http://localhost:8007/doc/swagger-ui/index.html,同时检查/v3/api-docs接口是否返回200状态码和正确的JSON格式API定义。
补充配置示例
如果需要自定义SpringDoc文档信息,可以添加如下配置类:
import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.info.Info; import org.springframework.context.annotation.Configuration; @Configuration @OpenAPIDefinition( info = @Info( title = "业务API文档", version = "1.0", description = "系统接口详细说明" ) ) public class SpringDocConfig { }
内容的提问来源于stack exchange,提问作者Martin DiCe
相关产品推荐
相关产品推荐

