Swagger-如何编写基于OpenAPI规范的API文档(详细操作)
一:打开swaggerhub官网登陆- 在线编辑器地址:https://swagger.io/tools/swaggerhub/
- 操作前先登陆账号,没有账号者先注册相应账号,有github账号者可直接登录
1-2-1 登陆
1-2-2 点击swaggerhub
1-2-3 github登陆
1-2-4创建新api
二:编写API
2-1 选择一份模板或者创建空的API
2-2 修改后保存
2-3
2-4 运行
2-5 执行结果
附代码示例:
openapi: 3.0.0
# Added by API Auto Mocking Plugin
servers:
- description: SwaggerHub API Auto Mocking
url: https://virtserver.swaggerhub.com/Aokyz/demo/1.0.0
info:
description: |
This is a sample Petstore server. You can find
out more about Swagger at
[http://swagger.io](http://swagger.io) or on
[irc.freenode.net, #swagger](http://swagger.io/irc/).
version: "1.0.0"
title: Swagger Petstore
termsOfService: 'http://swagger.io/terms/'
contact:
email: apiteam@swagger.io
license:
name: Apache 2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: 'http://swagger.io'
- name: store
description: Access to Petstore orders
- name: user
description: Operations about user
externalDocs:
description: Find out more about our store
url: 'http://swagger.io'
paths:
/pet:
get:
tags:
- pet
summary: Find pet by ID
description: Returns a single pet
operationId: getPetById
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
'400':
description: Invalid ID supplied
'404':
description: Pet not found
components:
schemas:
User:
type: object
properties:
id:
type: integer
description: The user ID.
username:
type: string
description: The user name.