swagger-yaml格式样例
时间: 2023-09-05 10:01:24 浏览: 101
Swagger-YAML是一种用于描述RESTful API的标记语言。它使用YAML格式来编写API的各种信息,包括API的基本信息、路径、操作和其他元数据。
以下是一个Swagger-YAML格式的示例:
```yaml
swagger: '2.0'
info:
version: 1.0.0
title: Pet Store API
description: This API provides endpoints for managing a pet store
paths:
/pets:
get:
summary: Get all pets
produces:
- application/json
responses:
200:
description: Successful operation
schema:
type: array
items:
$ref: '#/definitions/Pet'
post:
summary: Add a new pet
consumes:
- application/json
parameters:
- in: body
name: pet
description: Pet object that needs to be added to the store
schema:
$ref: '#/definitions/Pet'
responses:
201:
description: Pet created
/pets/{id}:
get:
summary: Get pet by ID
produces:
- application/json
parameters:
- name: id
in: path
required: true
type: integer
format: int64
responses:
200:
description: Successful operation
schema:
$ref: '#/definitions/Pet'
put:
summary: Update pet by ID
consumes:
- application/json
parameters:
- name: id
in: path
required: true
type: integer
format: int64
- in: body
name: pet
description: New details of the pet
schema:
$ref: '#/definitions/Pet'
responses:
200:
description: Pet updated
404:
description: Pet not found
definitions:
Pet:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
```
上述示例中,我们定义了一个名为Pet Store API的RESTful API,并定义了两个路径:/pets和/pets/{id}。每个路径下有对应的操作,如获取所有宠物、添加新宠物、通过ID获取特定宠物和更新特定宠物等。
Swagger-YAML格式使用了一些关键字来描述各种信息,比如info用于提供API的基本信息,paths用于定义各个路径下的操作,definitions用于定义数据模型等。
通过使用Swagger-YAML格式,我们可以清晰地描述API的结构和功能,并可以自动生成API文档、客户端代码和服务器模拟等。这样可以提高团队的协作效率,并方便了解和使用API。
阅读全文