-
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