概述
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for REST APIs, which allows both humans and computers to discover and understand the capabilities of a service without requiring access to source code, additional documentation, or inspection of network traffic. When properly defined via OpenAPI, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interface descriptions have done for lower-level programming, the OpenAPI Specification removes guesswork in calling a service.
簡單一句話,OpenAPI規範定義了一個標準、跟編程語言無關的rest API接口描述,它可以同時允許開發人員和操作系統查看並理解某個服務的功能,而無需訪問源代碼,文檔或網絡流量檢查(既方便人類學習和閱讀,也方便機器閱讀)。
當定義完OpenAPI,可以使用工具生成代碼。簡化開發。
數據類型
OpenAPI 的組件
OpenAPI文檔結構
Components內容
Path部分
生成api
當寫完這個api.yml,然後放在editor.swagger裏面,可得到下面的結果:
使用命令生成代碼
java -jar swagger-codegen-cli.jar generate -DdateLibrary=joda --api-package com.swagger.v1 --model-package com.swagger.v1.model --group-id demo1.swagger --artifact-id project-foundation-domain --artifact-version 0.1.0-SNAPSHOT --additional-properties useTags=true -l spring -i C:/Users/BinYang/Desktop/project/backend/learning-swagger.yml -o C:/Users/BinYang/Desktop/project/backend/swagger