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

Spring Boot3集成springdoc-openapi默认显示PetStore API问题求助

解决SpringDoc Swagger UI默认显示PetStore API的问题

针对你使用Spring Boot 3.0.2 + JDK17 + springdoc-openapi-starter-webmvc-ui 2.0.2时遇到的默认显示PetStore API的问题,可按以下步骤排查解决:

1. 修复依赖兼容性问题

你手动排除了springdoc自带的swagger-ui并单独引入4.8.1版本,这可能导致版本适配冲突,让disable-swagger-default-url配置失效。建议移除自定义的swagger-ui依赖,取消对springdoc自带swagger-ui的排除,使用starter包内置的适配版本:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.2</version>
    <!-- 移除之前的swagger-ui排除配置 -->
</dependency>

2. 调整配置文件,确保API文档路径正确

在application.yaml中补充API文档的路径配置,让Swagger UI能正确识别并加载你应用的API:

springdoc:
  api-docs:
    path: /v3/api-docs  # 指定自己应用的API文档接口路径
  swagger-ui:
    path: /swagger-ui.html
    disable-swagger-default-url: true
    defaultModelsExpandDepth: -1  # 可选,设为-1可隐藏默认展开的模型,简化界面

3. 确保应用存在可被扫描的API接口

SpringDoc会自动扫描带有@RestController、@RequestMapping等注解的控制器和接口方法生成文档。如果你的应用中没有任何API接口,Swagger UI可能仍会 fallback 到默认的PetStore示例,建议检查控制器类是否标注正确的注解。

4. 清理缓存并重启应用

删除项目的target或build目录,清理Maven/Gradle缓存后重启应用,确保新的依赖和配置生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 15:25:27