1. 程式人生 > >Swagger編寫API文件的YAML

Swagger編寫API文件的YAML

#必要欄位Swagger規範版本必須填2.0否則該YAML將不能用於Swagger其他元件swagger: '2.0'#必要欄位描述API介面資訊的元資料info: #介面標題 title: swagger說明文件  #介面文件的描述 description: 學習Swagger #版本號 version: 1.0.0#Swagger會提供測試用例host指定測試時的主機名如果沒有指定就是當前主機,可以指定埠host: 127.0.0.1#定義的api的字首必須已/開頭,測試用例的主機則為:hostbashPathbasePath: /api#指定呼叫介面的協議必須是:"http"
, "https", "ws", "wss"預設是http.-表示是個陣列元素即schemes接受一個數組引數schemes: - http - https#對應與http協議頭request的Accept呼叫者可接受型別,預設是*/*,定義的型別必須是http協議定義的 Mime Types,RestfulAPI一般定義成application/json#這兩個是對所有介面的全域性設定,在細化的介面中是還可以對應這兩個屬性來覆蓋全域性屬性produces: - application/json#必要欄位!定義可有可操作的APIpaths: /users: #必要欄位!定義HTTP操作方法,必須是http協議定義的方法
get: #介面概要 summary: 查詢所有使用者資訊 #介面描述 description: 查詢出所有使用者的所有資訊,使用者名稱,別名 #標籤,方便快速過濾出User相關的介面 tags: - User #返回值描述,必要自動 responses: #返回的http狀態碼 200: description: 所有使用者資訊或者使用者的集合資訊 #描述返回值 schema: #返回值格式,可選的有array,integer,string,boolean
type: array #針對array,每個條目的格式,type定義為array.必要填寫items items: #引用在definitions下定義的Users $ref: '#/definitions/User' #執行出錯的處理 default: description: 操作異常,執行失敗.返回資訊描述錯誤詳情 schema: #值型別 type: object #定義屬性 properties: #屬性名 message: #型別 type: string #即對於同一個url定義兩個不同的方法,表示兩個介面 post: description: 註冊一個使用者 #請求引數 parameters: #引數key - name: username #傳遞方法,formData表示表單傳輸,還有query表示url拼接傳輸,path表示作為url的一部分 #body表示http頭承載引數(body只能有一個,有body不能在有其他的) in: formData #引數描述 description: 使用者名稱,不能使用已經被註冊過的 #引數是否必要,預設false required: true #引數型別,可選的包括array,integer,boolean,string.使用array必須使用items type: string - name: password in: formData description: 使用者登陸密碼,加密傳輸,加密儲存 required: true type: string - name: alias in: formData type: string description: 使用者別名 #非必要欄位 required: false responses: #返回的http狀態碼 200: description: 通過返回值來標示執行結果 返回true表示執行成功 schema: #值型別 type: object #定義屬性 properties: #屬性名 status: #型別 type: boolean #描述 description: 是否成功 #執行出錯的處理 default: description: 操作異常,執行失敗.返回資訊描述錯誤詳情 schema: #值型別 type: object #定義屬性 properties: #屬性名 message: #型別 type: string /users/{id}: #{id}表示id為請求引數,例如/users/1,/users/2都是對該API的請求,此時id即為1和2 get: summary: 根據使用者名稱id查詢該使用者的所有資訊 description: 查詢出某個使用者的所有資訊,使用者名稱,別名等 tags: - User parameters: #上面介面中定義了{id},則引數列表中必須包含引數id,並且請求型別為path - name: id in: path description: 要查詢的使用者的使用者名稱,它是唯一標識 required: true type: string responses: 200: description: 所有使用者資訊或者使用者的集合資訊 schema: $ref: '#/definitions/User' default: description: 操作異常,執行失敗.返回資訊描述錯誤詳情 schema: #值型別 type: object #定義屬性 properties: #屬性名 message: #型別 type: string #http定義的delete方法,刪除一個資源 delete: summary: 刪除使用者 description: 刪除某個使用者的資訊,被刪除的使用者將無法登陸 parameters: - name: id in: path type: string required: true description: 使用者的唯一標示符 tags: - User responses: 200: description: 通過返回值來標示執行結果 返回true表示執行成功 schema: #值型別 type: object #定義屬性 properties: #屬性名 status: #型別 type: boolean #描述 description: 是否成功 default: description: 操作異常,執行失敗.返回資訊描述錯誤詳情 schema: #值型別 type: object #定義屬性 properties: #屬性名 message: #型別 type: string #描述錯誤資訊 #http定義的patch方法,表示修改一個資源 patch: summary: 使用者資訊修改 description: 修改使用者資訊(使用者名稱別名) parameters: - name: id in: path description: 使用者名稱,要修改的資料的唯一識別符號 required: true type: string - name: alias in: formData description: 新的使用者別名 required: true type: string tags: - User responses: 200: description: 通過返回值來標示執行結果 返回true表示執行成功 schema: #值型別 type: object #定義屬性 properties: #屬性名 status: #型別 type: boolean #描述 description: 是否成功 default: description: 操作異常,執行失敗.返回資訊描述錯誤詳情 schema: #值型別 type: object #定義屬性 properties: #屬性名 message: #型別 type: string #描述錯誤資訊definitions: User: #值型別 type: object #定義屬性 properties: #屬性名 id: #型別 type: string #描述 description: 使用者的唯一id username: type: string description: 使用者名稱 alias: type: string description: 別名