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

Spring Boot集成OpenApi与Swagger找不到swagger.json报错如何解决

问题原因

你同时引入了springdoc-openapi-ui和springfox-boot-starter两套OpenAPI/Swagger实现依赖,两个框架默认都会注册/v3/api-docs接口的请求处理器,因此出现同一路径的映射冲突,导致接口无法正常返回swagger.json内容,Swagger UI无法加载渲染所需的接口数据。

解决方案

两个方案二选一即可,禁止同时保留两套依赖:

方案1:保留springdoc,删除springfox依赖

  • 移除pom.xml中的springfox-boot-starter依赖,仅保留springdoc-openapi-ui依赖即可,你现有的OpenAPI配置类无需修改
  • 启动项目后访问http://{服务ip}:{服务端口}/swagger-ui.html即可正常使用
  • 如需自定义接口路径,可在application配置文件中添加如下配置:
springdoc:
  api-docs:
    path: /v3/api-docs # 可自定义为任意路径
  swagger-ui:
    path: /swagger-ui.html

方案2:保留springfox,删除springdoc依赖

  • 移除pom.xml中的springdoc-openapi-ui依赖,仅保留springfox-boot-starter依赖
  • 调整Swagger配置类适配springfox的API规范,示例如下:
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                // 替换为你项目Controller层的包路径
                .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Service API")
                .version("1")
                .description("Service")
                .build();
    }
}
  • 启动项目后访问http://{服务ip}:{服务端口}/swagger-ui/index.html即可正常使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 12:15:03