{"title": "Swagger Sample App","description": "This is a sample server Petstore server.","termsOfService": "http://swagger.io/terms/","contact": {"name":"API Support","url":"http://www.swagger.io/support","email":"support@swagger.io" },"license": {"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html" },"version": "1.0.1"}
title: Swagger Sample Appdescription: This is a sample server Petstore server.termsOfService: http://swagger.io/terms/contact: name:API Support url: http://www.swagger.io/support email: support@swagger.iolicense: name: Apache 2.0 url: http://www.apache.org/licenses/LICENSE-2.0.htmlversion:1.0.1
{"/pets": {"get": {"description":"Returns all pets from the system that the user has access to","produces": ["application/json" ],"responses": {"200": {"description":"A list of pets.","schema": {"type":"array","items": {"$ref":"#/definitions/pet" } } } } } }}
/pets: get: description: Returns all pets from the system that the user has access to produces:- application/json responses:'200': description:A list of pets. schema: type: array items: $ref:'#/definitions/pet'
{"get": {"description":"Returns pets based on ID","summary":"Find pets by ID","operationId":"getPetsById","produces": ["application/json","text/html" ],"responses": {"200": {"description":"pet response","schema": {"type":"array","items": {"$ref":"#/definitions/Pet" } } },"default": {"description":"error payload","schema": {"$ref":"#/definitions/ErrorModel" } } } },"parameters": [ {"name":"id","in":"path","description":"ID of pet to use","required":true,"type":"array","items": {"type":"string" },"collectionFormat":"csv" } ]}
get: description: Returns pets based on ID summary: Find pets by ID operationId: getPetsById produces:- application/json- text/html responses:'200': description: pet response schema: type: array items: $ref:'#/definitions/Pet'default: description: error payload schema: $ref:'#/definitions/ErrorModel'parameters:- name: id in: path description:IDof pet to use required:true type: array items: type: string collectionFormat: csv
Operation 对象(Operation Object)
描述路径上的一个API操作。
固定字段(Fixed Fields)
通配对象(Patterned Objects)
Operation 对象示例:
{"tags": ["pet" ],"summary": "Updates a pet in the store with form data","description": "","operationId": "updatePetWithForm","consumes": ["application/x-www-form-urlencoded" ],"produces": ["application/json","application/xml" ],"parameters": [ {"name":"petId","in":"path","description":"ID of pet that needs to be updated","required":true,"type":"string" }, {"name":"name","in":"formData","description":"Updated name of the pet","required":false,"type":"string" }, {"name":"status","in":"formData","description":"Updated status of the pet","required":false,"type":"string" } ],"responses": {"200": {"description":"Pet updated." },"405": {"description":"Invalid input" } },"security": [ {"petstore_auth": ["write:pets","read:pets" ] } ]}
tags:- petsummary: Updates a pet in the store with form datadescription:""operationId: updatePetWithFormconsumes:- application/x-www-form-urlencodedproduces:- application/json- application/xmlparameters:- name: petId in: path description:IDof pet that needs to be updated required:true type: string- name: name in: formData description: Updated name of the pet required:false type: string- name: status in: formData description: Updated status of the pet required:false type: stringresponses:'200': description: Pet updated.'405': description: Invalid inputsecurity:- petstore_auth:- write:pets- read:pets
外部文档对象(External Documentation Object)
允许引用外部资源来扩展文档。
固定字段(Fixed Fields)
通配对象(Patterned Objects)
外部文档对象示例:
{"description": "Find more info here","url": "https://swagger.io"}
description: Find more info hereurl: https://swagger.io
Body - 附加到HTTP请求的payload。因为只有一个payload,所以只能有一个Body参数。body参数的名称对参数本身没有影响,仅用于文档目的。考虑到Form本身也是一种Body,所以对于同一个操作,Form和Body不能共存。
Form - 当请求的contentType为application/x-www-form-urlencoded或者application/form-data或者两者都有时(在Swagger的定义中,操作的consumes属性)。用于描述附加在HTTP请求上的payload。这是唯一可以用来发送文件的参数类型,从而支持文件类型。由于在payload中发送了表单参数,因此无法将它们与相同操作的body参数一起声明。表单参数的格式与使用的内容类型有不同(为了进一步的细节,请参考http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4):