Spring Boot 3.2.2配置springdoc-openapi遇Swagger UI 404错误求助
解决Spring Boot集成springdoc-openapi-ui 404问题
1. 检查版本兼容性
springdoc-openapi-starter-webmvc-ui 2.3.0仅适配Spring Boot 3.0及以上版本。如果你的项目用的是Spring Boot 2.x,直接使用该版本会导致静态资源加载失败:
- 若使用Spring Boot 3.x:确认
pom.xml中spring-boot-starter-parent版本≥3.0.0 - 若使用Spring Boot 2.x:替换依赖为适配2.x的版本,配置如下:
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.6.14</version> </dependency>
2. 修正swagger-ui访问路径
springdoc 2.x的默认访问路径是http://localhost:8080/swagger-ui.html,而非你访问的/swagger-ui/index.html,直接访问这个默认路径大概率能解决404问题。
3. 排查Maven打包问题
你提到jar包中META-INF下没有resource文件夹,其实springdoc的静态资源是打包在自身依赖jar中的(路径为BOOT-INF/lib/springdoc-openapi-starter-webmvc-ui-2.3.0.jar内部的META-INF/resources/swagger-ui),并非项目自身的META-INF目录:
- 检查
pom.xml的spring-boot-maven-plugin是否有自定义配置导致依赖资源被排除,确保插件配置没有覆盖默认行为:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- 移除任何可能排除依赖资源的配置,比如<exclude>标签 --> </plugin> </plugins> </build>
- 执行
mvn clean package重新打包,解压jar包确认上述springdoc依赖内部的资源是否存在。
4. 简化配置并确认Controller扫描
- 暂时注释
application.yml中所有springdoc相关配置,使用默认配置启动,避免自定义路径导致冲突。 - 确认你的
@RestController类被Spring Boot扫描到:主类的@SpringBootApplication默认扫描同包及子包,若Controller在其他包,添加@ComponentScan指定扫描路径。
5. 修复损坏的依赖
如果以上步骤都无效,删除本地Maven仓库中org/springdoc目录下的所有文件,执行mvn clean install重新下载依赖,避免依赖包损坏导致资源缺失。
内容的提问来源于stack exchange,提问作者Anam Qureshi
相关产品推荐
相关产品推荐

