Swagger Editor编写

一、Swagger Editor编辑器

1.在线编写

https://editor.swagger.io/
其是官方提供的编写swagger yaml文件的在线编辑器,且可以实时看见编写的yaml文件的可视化页面

2.IDEA安装插件

在插件库中搜索swagger,会出现一堆swagger editor相关的插件,本文选择的是 OpenAPI(Swagger)Editor。
安装插件并重启idea后,新建yaml文件即可进行编写,如下图:

3.两种方式优缺点

在线:无提示与智能补全,有可视化页面,编写的同时会及时展示可视化界面。另外,其有比较好的报错提示。
idea:有提示与智能补全,有可视化页面,但可视化界面反应较慢。其错误提示较差。

建议两种方式搭配使用:idea编写、在线查看。

二、语法规则

1.字段类型与格式定义

在这里插入图片描述

Swagger API包含以下部分:

参数包含的参数部分含义
swagger必需,指定swagger spec版本,2.0
info必需,提供API的元数据
tags建议必需,补充的元数据,在swagger ui中,用于作为api的分组标签
host必需,主机、域名,如果没有提供,则使用文档所在的host
basePath相对于host的路径
schemes必需,API的传输协议,http,https,ws,wss
consumesAPI可以消费的MIME类型列表
producesAPI产生的MIME类型列表
pathsAPI的路径,以及每个路径的HTTP方法,一个路径加上一个HTTP方法构成了一个操作。每个操作都有以下内容:
tags操作的标签
summary短摘要
description描述
externalDocs外部文档
operationId标识操作的唯一字符串
consumesMIME类型列表
producesMIME类型列表
parameters参数列表
responses应答状态码等
schemes传输协议
deprecated是否弃用,默认false,不推荐使用
security安全
definitions定义API消费或生产的数据类型,使用json-schema描述,操作的parameter和response部分可以通过引用的方式使用definitions部分定义的schema
parameters多个操作共用的参数
responses多个操作共用的响应
securityDefinitions安全scheme定义
security安全声明
externalDocs附加的外部文档

详细可见官网:
https://swagger.io/docs/specification/2-0/basic-structure/

另外注意排版,其语法要求严格对齐。

三、示例说明

1.编写get-yaml示例

文件取名自定义,建议有标识度,以.yaml结尾,如:DeviceQuery.yaml

##必要字段!Swagger规范版本,必须填2.0,否则该YAML将不能用于Swagger其他组件
swagger: "2.0"
#必要字段!描述API接口信息的元数据
info:
  #接口标题
  title: 设备管理服务
  #接口文档的描述
  description: 包含设备管理接口
  #版本号,自定义
  version: 1.0.0
#服务地址,必要,之后统一填集团能力桥接服务网关地址
host: 127.0.0.1:30006
##定义的api的前缀,必须/开头,可以不设置该参数,建议设置
basePath: /iot/device/get
#接口分类标签,建议设置,可以定义多个,-表示是个数组元素
tags:
  - name: device
    description: 设备管理
# 指定调用接口的协议,必须是:"http", "https", "ws", "wss".
#  -表示是个数组元素,即schemes接受一个数组参数
schemes:
  - http
#对应与http协议头request的Accept,调用者产生类型。
#默认是*/*,定义的类型必须是http协议定义的 Mime Types
#RestfulAPI一般定义成application/json
#这两个是对所有接口的全局设置
#在细化的接口中还可以对应这两个属性来覆盖全局属性
produces:
  - application/json
#必要字段!定义可操作的API
paths:
  #具体url接口调用路径(若是上文中没有设置basePath的话,这里就需要写全为/iot/device/get/test)
  /test:
    ##必要字段!定义HTTP操作方法,必须是http协议定义的方法(get、post、put、delete)
    get:
      #上面tags中定义的tag标签,方便快速过滤出某种标签下的接口
      tags:
        - device
      ##接口概要
      summary: 查询普通设备详情
      #接口描述
      description: 查询普通设备的详细信息
      #接口唯一标识,可以类似于接口的方法名
      operationId: queryDevice
      #接口产生类型,局部生效
      produces:
        - application/json
      #请求参数
      parameters:
         ##参数key,该参数为deviceId
        - name: deviceId
          ## 传递方法,formData表示表单传输,
          ##  query表示url拼接传输,
          ##  path表示作为url的一部分
          ##  body表示http头承载参数(body只能有一个,有body不能再有其他的)
          ##  一般用query(url拼接传参)、body(类似body体传参)
          in: query
          #是否必传
          required: true
          #参数类型
          type: string
          #描述
          description: 设备ID
          #是否允许传空值,默认false,可以不设置
          allowEmptyValue: false
      ##返回值描述,必要
      responses:
        ##返回的http状态码,自定义
        '200':
          description: "successful operation"
          ##描述返回值
          schema:
            ##引用在definitions下定义的ResultBean公共参数,注意此时后面不能再接description属性,因为其definitions下定义ResultBean时已经有了description属性
            $ref: "#/definitions/ResultBean"
        "401":
          description: "Unauthorized"
        "403":
          description: "Forbidden"
        "404":
          description: "Not Found"
        default:
          description: "Unexpected error"
#定义公共参数
definitions:
  #参数名,可以类似于实体类的ResultBean类
  ResultBean:
    #类型:string、interger、object、array、boolean、number
    type: object
    #属性标题,建议与上面的参数名等同
    title: ResultBean
    #属性描述
    description: 接口请求返回结果
    #哪些必传字段
    required:
      - data
      - errorCode
      - errorMsg
    ##定义属性
    properties:
      #属性名,自定义的data字段
      data:
        #引用在definitions下定义的DeviceInfo公共参数
        $ref: "#/definitions/DeviceInfo"
      errorCode:
        #类型
        type: integer
        #格式
        format: int32
        description: 请求结果code
      errorMsg:
        type: "string"
        description: "请求结果描述"
  #设置参数名,可以类似于实体类的DeviceInfo类
  DeviceInfo:
    type: object
    title: DeviceInfo
    description: 设备详情
    required:
      - id
      - serialNumber
      - deviceId
      - deviceKey
      - projectCode
      - venderId
      - corporation
      - corporationText
      - model
      - modelText
      - fwVersion
      - cpVersion
      - protocolCode
      - deviceName
      - deviceType
      - ipAddr
      - port
      - status
      - acquisitionFrequency
      - isOnline
    properties:
      id:
        type: integer
        format: int64
        description: 主键ID
        #默认值设为1
        default: 1
      serialNumber:
        type: string
        description: 唯一编号或mac(不带“-”)
      deviceId:
        type: string
        description: 设备ID
      deviceKey:
        type: string
        description: 设备密钥
      projectCode:
        type: string
        description: 项目编号
      venderId:
        type: string
        description: 供应商ID
      corporation:
        type: string
        description: 厂家编号
      corporationText:
        type: string
        description: 厂家名称
      model:
        type: string
        description: 型号编号
      modelText:
        type: string
        description: 型号名称
      fwVersion:
        type: string
        description: 固件版本号
      cpVersion:
        type: string
        description: 组件版本号
      protocolCode:
        type: string
        description: 对接协议版本号
      deviceName:
        type: string
        description: 设备名称
      deviceType:
        type: string
        description: 设备类型
      ipAddr:
        type: string
        description: IP地址
      port:
        type: integer
        format: int32
        description: 端口号
      status:
        type: integer
        format: int32
        description: 状态,1:有效,9:已刪除
      acquisitionFrequency:
        type: integer
        format: int32
        description: 设备心跳频率,单位分钟,默认10分钟
        default: 10
      isOnline:
        type: integer
        format: int32
        description: 是否在线,0:不在线,1:在线
        #枚举
        enum:
          - 0
          - 1

1.1可视化界面

在这里插入图片描述

2.编写post-yaml示例

文件取名自定义,建议有标识度,以.yaml结尾,如:PersonLibraryQuery.yaml

##必要字段!Swagger规范版本,必须填2.0,否则该YAML将不能用于Swagger其他组件
swagger: "2.0"
#必要字段!描述API接口信息的元数据
info:
  #接口标题
  title: 静态人脸识别服务
  #接口文档的描述
  description: 静态人脸识别接口
  #版本号,自定义
  version: 1.0.0
#服务地址,必要,之后统一填集团能力桥接服务网关地址
host: "127.0.0.1:6061"
##定义的api的前缀,必须/开头,可以不设置该参数,建议设置
basePath: /ai/face/dataset/get
#接口分类标签,建议设置,可以定义多个,-表示是个数组元素
tags:
  - name: personLibrary
    description: 人像库接口
# 指定调用接口的协议,必须是:"http", "https", "ws", "wss".
#  -表示是个数组元素,即schemes接受一个数组参数
schemes:
  - http
#必要字段!定义可操作的API
paths:
  #具体url接口调用路径(若是上文中没有设置basePath的话,这里就需要写全为/ai/face/dataset/get/test)
  /test:
    ##必要字段!定义HTTP操作方法,必须是http协议定义的方法(get、post、put、delete)
    post:
      #上面tags中定义的tag标签,方便快速过滤出某种标签下的接口
      tags:
        - personLibrary
      ##接口概要
      summary: 人像库查询
      #接口描述
      description: 查询人像库的详细信息
      #接口唯一标识,可以类似于接口的方法名
      operationId: queryPersonLibrary
      #接口产生类型,局部生效
      produces:
        - application/json
      #请求参数
      parameters:
        #参数key,该参数为名为params的body体
        - name: params
          ## 传递方法,formData表示表单传输,
          ##  query表示url拼接传输,
          ##  path表示作为url的一部分
          ##  body表示http头承载参数(body只能有一个,有body不能在有其他的)
          ##  一般用query(url拼接传参)、body(类似body体传参)
          in: body
          #是否必传
          required: true
          #参数描述
          description: 入参
          schema:
            #引用在definitions下定义的QueryBo公共参数
            $ref: "#/definitions/QueryBo"
      ##返回值描述,必要
      responses:
        ##返回的http状态码,自定义
        '200':
          description: "successful operation"
          schema:
            ##引用在definitions下定义的ResultBean公共参数,注意此时后面不能再接description属性,因为其definitions下定义ResultBean时已经有了description属性
            $ref: "#/definitions/ResultBean"
        "401":
          description: "Unauthorized"
        "403":
          description: "Forbidden"
        "404":
          description: "Not Found"
        default:
          description: "Unexpected error"
#定义公共参数
definitions:
  #参数名,可以类似于实体类的QueryBo类
  QueryBo:
    #类型:string、interger、object、array、boolean、number
    type: object
    #属性标题,建议与上面的参数名等同
    title: QueryBo
    #属性描述
    description: 查询入参
    #哪些必传字段
    required:
      - name
      - app_id
    ##定义属性
    properties:
      #属性名,自定义的name字段
      name:
        type: string
        description: 人脸库名称
        default: renxiang
      #属性名,自定义的app_id字段
      app_id:
        type: string
        description: appid
        default: appid
      #属性名,自定义的pageSize字段
      pageSize:
        #类型
        type: integer
        #格式
        format: int32
        description: 每页显示条数
        #默认值
        default: 10
      #属性名,自定义的pageNum字段
      pageNum:
        type: integer
        format: int32
        description: 起始页
        default: 1
  #参数名,可以类似于实体类的ResultBean类
  ResultBean:
    type: object
    title: ResultBean
    description: 接口请求返回结果
    required:
      - data
      - errorCode
      - errorMsg
    properties:
      #属性名,自定义的data字段
      data:
        #类型,类似于arraylist
        type: array
        description: 请求结果数据
        #array的组成部分
        items:
          #引用在definitions下定义的PersonLibraryInfo公共参数
          $ref: "#/definitions/PersonLibraryInfo"
      #属性名,自定义的errorCode字段
      errorCode:
        type: integer
        format: int32
        description: 请求结果code
      #属性名,自定义的errorMsg字段
      errorMsg:
        type: string
        description: 请求结果描述
  PersonLibraryInfo:
    type: object
    title: PersonLibraryInfo
    description: 人像库信息
    required:
      - repositoryId
      - name
    properties:
      repositoryId:
        type: integer
        format: int64
        description: 人脸库id
      name:
        type: string
        description: 人脸库名称

2.1可视化界面

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值