-
swagger: '2.0'
info:
version: 1.0.0
title: Simple API
description: A simple API documentation
schemes:
- https
host: simple.api
basePath: /openapi101
paths:
{}
- 顯示界面如下:

- 首先要通過swagger屬性來聲明OpenAPI規范的版本
- 然后需要說明API文檔的相關信息,比如API文檔版本、API文檔名稱、描述信息等
- 最后作為webURL,一個很重要的信息就是用來給消費者使用的 根URL ,可以使用協議http或https、主機名、根路徑來描述:
schemes:
- https
host: simple.api
basePath: /openapi101
```
- 接下來就是寫API的操作,通過paths,然而這里沒有寫只是通過{}對象占用位置
-
swagger: '2.0'
info:
version: 1.0.0
title: Simple API
description: A simple API documentation
schemes:
- https
host: simple.api
basePath: /openapi101
paths:
/persons:
get:
summary: Get some persons
description: Returns a list containing all persons
responses:
200:
description: A list of Person
schema:
type: array
items:
required:
- username
properties:
firstname:
type: string
lastname:
type: string
username:
type: string
- 在上面的這些代碼中,做了以下的動作:
- 添加了/persons的路徑,用來訪問一組用戶信息
- 在路徑中添加了HTTP方法get,同時也有一些簡單的描述信息summary和description
- 定義響應類型responses,響應類型中添加了HTTP狀態碼200
- 定義了響應內容:通過響應消息中的schema屬性來描述清楚具體的返回內容。通過type屬性可知一組用戶信息就是一個用戶信息數組,每一個數組元素則是一個用戶對象,該對象包含三個string類型的屬性,其中username必須提供(required)
- 定義請求參數
-
paths:
/persons:
get:
summary: Get some persons
description: Returns a list containing all persons
parameters:
- name: pageSize
in: query
description: Number of persons returned
type: integer
- name: pageNumber
in: query
description: Page number
type: integer
- 首先在get方法中添加了一個parameters屬性
- 在參數列表中,添加了兩個參數pageSize和pageNumber的整形參數,並有簡單描述
- 定義路徑參數
-
swagger: '2.0'
info:
version: 1.0.0
title: Simple API
description: A simple API documentation
schemes:
- https
host: simple.api
basePath: /openapi101
paths:
/persons/{username}:
get:
summary: Get some persons
description: Returns a list containing all persons
parameters:
- name: username
in: path
required: true
description: The person's username
type: string
responses:
200:
description: A list of Person
schema:
type: array
items:
required:
- username
properties:
firstname:
type: string
lastname:
type: string
username:
type: string
- 路徑參數、請求參數以及消息參數等的不同之處就在於in屬性的值不同,分別為path、query、body等。同時對於參數的類型可以使用type或者schema來定義,例如消息體參數如下:
-
swagger: '2.0'
info:
version: 1.0.0
title: Simple API
description: A simple API documentation
schemes:
- https
host: simple.api
basePath: /openapi101
paths:
/persons:
post:
summary: Creates a person
description: Adds a new person to the persons list
parameters:
- name: person
in: body
description: The person to create
schema:
required:
- username
properties:
firstname:
type: string
lastname:
type: string
username:
type: string
responses:
200:
description: OK
- 如果是單個參數可以使用type進行定義例如integer,string ,array等,而如果是json類型的參數就需要使用schema類來定義。
- 定義相應消息
-
response:
204:
description:Persons successfully created
400:
description:Persons couldn't have been created
- 簡化數據模型
- 通過使用definition來定義可重用的對象。如下:
-
swagger: '2.0'
info:
version: 1.0.0
title: Simple API
description: A simple API documentation
schemes:
- https
host: simple.api
basePath: /openapi101
paths:
/persons:
post:
summary: Creates a person
description: Adds a new person to the persons list
parameters:
- name: person
in: body
description: The person to create
schema:
$ref: '#/definitions/Person'
responses:
200:
description: OK
schema:
$ref: "#/definitions/Person"
definitions:
Person:
required:
- username
properties:
firstname:
type: string
lastname:
type: string
username:
type: string
- 定義可重用的參數
-
swagger: '2.0'
info:
version: 1.0.0
title: Simple API
description: A simple API documentation
schemes:
- https
host: simple.api
basePath: /openapi101
paths:
/persons/{username}:
get:
parameters:
- $ref: '#/parameters/username'
responses:
200:
description: fsafsf
parameters:
username:
name: username
in: path
required: true
description: The person's username
type: string