SpringDoc OpenAPI 配置問題

张开发
2026/4/20 14:17:20 15 分钟阅读

分享文章

SpringDoc OpenAPI 配置問題
簡介OpenAPI 提供了標準化的規範讓開發者能夠以 json 或 yaml 格式來描述 API 規格。Springdoc OpenAPI 是一個專門為 Spring Boot REST API 自動產生 API 文件的工具讓你不需要手動寫 Swagger 設定就能快速生成互動式文件頁面。參照 OpenAPI SpecificationOAS加入依賴Mavenpom.xmlmodelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.5.13/version relativePath/ /parent groupIdcom.yudanny/groupId artifactIdyumicro/artifactId version0.0.1-SNAPSHOT/version nameyumicro/name description/ packagingpom/packaging modules moduleaccounts/module /modules dependency groupIdorg.springdoc/groupId artifactIdspringdoc-openapi-starter-webmvc-ui/artifactId version2.8.4/version /dependency註: 使用Java x與Spring Boot 3.x.x有時候 SpringDoc 可能需要相容性微調。RestController RequestMapping(path /api, produces { MediaType.APPLICATION_JSON_VALUE }) AllArgsConstructor Validated public class AccountsController { private IAccountsService iAccountsService; Operation(summary Create an account) Tag(name Account, description Account Management API) PostMapping(/create) public ResponseEntityResponseDto createAccount(Valid RequestBody CustomerDto customerDto) { iAccountsService.createAccount(customerDto); return ResponseEntity .status(HttpStatus.CREATED) .body(new ResponseDto(AccountsConstants.STATUS_201, AccountsConstants.MESSAGE_201)); } . . . }問題「在多模組專案中Parent POM 的依賴管理如何正確傳遞到子模組的 Runtime」http://localhost:8080/swagger-ui/index.htmlSpringDoc OpenAPI 配置問題。當你看到 Failed to load API definition 時通常意味著 Swagger UI 頁面雖然跑起來了但它去抓取後端 v3/api-docsJSON 定義檔時失敗了。可檢查以下幾個點1.檢查 API Definition 的實際路徑Swagger UI 預設請求 /v3/api-docs。在瀏覽器直接輸入 http://localhost:8070/v3/api-docs如果出現 404 代表 SpringDoc 沒有成功掃描到你的 Controller。如果出現 JSON 代表 Swagger UI 配置的路徑不對。2.修正application.yml配置springdoc: swagger-ui: path: /swagger-ui.html api-docs: path: /v3/api-docs3.多模組專案的依賴問題在多模組架構中如果在 Parent 定義了 springdoc-openapi-starter-webmvc-ui確保子模組 accounts 的 pom.xml 確實有繼承到這個依賴。正常顯示

更多文章