Spring Boot Gradle项目中OpenApi文档无法生成问题求助
问题解决步骤
你的核心问题是缺少springdoc-openapi的运行时核心依赖——你只添加了Gradle插件,但该插件仅用于离线生成静态OpenAPI文档文件,无法在应用运行时提供/api-docs和Swagger UI的访问端点。
1. 添加springdoc-openapi Starter依赖
修改build.gradle的dependencies块,添加适配Spring Boot 3.x的Web MVC Starter依赖:
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' // 添加这行核心依赖 implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
注:版本
2.2.0是适配Spring Boot 3.x的稳定版本,可根据实际需求调整为同系列的最新版本。
2. 重新构建并运行应用
执行以下命令重新构建项目并启动:
./gradlew clean build java -jar build/libs/name-of-jar-file.jar
3. 验证访问
应用启动后,访问以下URL即可正常查看文档:
- OpenAPI JSON文档:
http://localhost:8080/sample-service/api-docs - Swagger UI界面:
http://localhost:8080/sample-service/swagger-ui-custom.html
额外说明
org.springdoc.openapi-gradle-plugin插件的作用是离线生成OpenAPI文档(比如执行./gradlew generateOpenApiDocs会在build/openapi目录下生成静态JSON/YAML文件),和运行时的端点服务是两个独立的功能。- 确保你的控制器类上添加了Spring MVC的注解(如
@RestController、@GetMapping等),springdoc会自动扫描这些注解生成API文档。
内容的提问来源于stack exchange,提问作者aCarella
相关产品推荐
相关产品推荐

