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
相关产品推荐
相关产品推荐

