ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Swagger编写API文档的YAML中文示例

2021-06-03 03:01:21  阅读:317  来源: 互联网

标签:description 示例 定义 YAML API 属性 type id string


必要字段!Swagger规范版本,必须填2.0,否则该YAML将不能用于Swagger其他组件

swagger: '2.0'

必要字段!描述API接口信息的元数据

info:

接口标题

title: swagger说明文档 

接口文档的描述

description: 学习Swagger

版本号

version: 1.0.0

Swagger会提供测试用例,host指定测试时的主机名,如果没有指定就是当前主机,可以指定端口.

host: 127.0.0.1

定义的api的前缀,必须已/开头,测试用例的主机则为:host+bashPath

basePath: /api

指定调用接口的协议,必须是:"http", "https", "ws", "wss".默认是http.-表示是个数组元素,即schemes接受一个数组参数

schemes:

  • http
  • https

对应与http协议头request的Accept,调用者可接受类型,默认是/,定义的类型必须是http协议定义的 Mime Types,RestfulAPI一般定义成application/json

这两个是对所有接口的全局设置,在细化的接口中是还可以对应这两个属性来覆盖全局属性

produces:

  • application/json

必要字段!定义可有可操作的API

paths:
/users:

必要字段!定义HTTP操作方法,必须是http协议定义的方法

get:
  #接口概要
  summary: 查询所有用户信息
  #接口描述
  description: 查询出所有用户的所有信息,用户名,别名
  #标签,方便快速过滤出User相关的接口
  tags:
    - User
  #返回值描述,必要自动
  responses:
    #返回的http状态码
    200:
      description: 所有用户信息或者用户的集合信息
      #描述返回值
      schema:
        #返回值格式,可选的有array,integer,string,boolean
        type: array
        #针对array,每个条目的格式,type定义为array.必要填写items
        items:
          #引用在definitions下定义的Users
          $ref: '#/definitions/User'
    #执行出错的处理
    default:
      description: 操作异常,执行失败.返回信息描述错误详情
      schema:
        #值类型
        type: object
        #定义属性
        properties:
        #属性名
          message:
            #类型
            type: string
#即对于同一个url定义两个不同的方法,表示两个接口
post:
  description: 注册一个用户
  #请求参数
  parameters:
      #参数key
    - name: username
      #传递方法,formData表示表单传输,还有query表示url拼接传输,path表示作为url的一部分
      #body表示http头承载参数(body只能有一个,有body不能在有其他的)
      in: formData
      #参数描述
      description: 用户名,不能使用已经被注册过的
      #参数是否必要,默认false
      required: true
      #参数类型,可选的包括array,integer,boolean,string.使用array必须使用items
      type: string
    - name: password
      in: formData
      description: 用户登陆密码,加密传输,加密存储
      required: true
      type: string
    - name: alias
      in: formData
      type: string
      description: 用户别名
      #非必要字段
      required: false
  responses:
    #返回的http状态码
    200:
      description: 通过返回值来标示执行结果 返回true表示执行成功
      schema:
         #值类型
          type: object
          #定义属性
          properties:
          #属性名
            status:
              #类型
              type: boolean
              #描述
              description: 是否成功
    #执行出错的处理
    default:
      description: 操作异常,执行失败.返回信息描述错误详情
      schema:
        #值类型
        type: object
        #定义属性
        properties:
        #属性名
          message:
            #类型
            type: string

/users/{id}:
#{id}表示id为请求参数,例如/users/1,/users/2都是对该API的请求,此时id即为1和2
get:
summary: 根据用户名id查询该用户的所有信息
description: 查询出某个用户的所有信息,用户名,别名等
tags:
- User
parameters:
#上面接口中定义了{id},则参数列表中必须包含参数id,并且请求类型为path
- name: id
in: path
description: 要查询的用户的用户名,它是唯一标识
required: true
type: string
responses:
200:
description: 所有用户信息或者用户的集合信息
schema:
$ref: '#/definitions/User'
default:
description: 操作异常,执行失败.返回信息描述错误详情
schema:
#值类型
type: object
#定义属性
properties:
#属性名
message:
#类型
type: string
#http定义的delete方法,删除一个资源
delete:
summary: 删除用户
description: 删除某个用户的信息,被删除的用户将无法登陆
parameters:
- name: id
in: path
type: string
required: true
description: 用户的唯一标示符
tags:
- User
responses:
200:
description: 通过返回值来标示执行结果 返回true表示执行成功
schema:
#值类型
type: object
#定义属性
properties:
#属性名
status:
#类型
type: boolean
#描述
description: 是否成功
default:
description: 操作异常,执行失败.返回信息描述错误详情
schema:
#值类型
type: object
#定义属性
properties:
#属性名
message:
#类型
type: string
#描述错误信息
#http定义的patch方法,表示修改一个资源
patch:
summary: 用户信息修改
description: 修改用户信息(用户名别名)
parameters:
- name: id
in: path
description: 用户名,要修改的数据的唯一标识符
required: true
type: string
- name: alias
in: formData
description: 新的用户别名
required: true
type: string
tags:
- User
responses:
200:
description: 通过返回值来标示执行结果 返回true表示执行成功
schema:
#值类型
type: object
#定义属性
properties:
#属性名
status:
#类型
type: boolean
#描述
description: 是否成功
default:
description: 操作异常,执行失败.返回信息描述错误详情
schema:
#值类型
type: object
#定义属性
properties:
#属性名
message:
#类型
type: string
#描述错误信息
definitions:
User:
#值类型
type: object
#定义属性
properties:
#属性名
id:
#类型
type: string
#描述
description: 用户的唯一id
username:
type: string
description: 用户名
alias:
type: string
description: 别名
————————————————
版权声明:本文为CSDN博主「u010466329」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u010466329/article/details/78522992

标签:description,示例,定义,YAML,API,属性,type,id,string
来源: https://www.cnblogs.com/zhang-blog/p/14843572.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有