【问题标题】:How to create api using post request with json as payload using swagger如何使用 swagger 使用 json 作为有效负载的 post 请求创建 api
【发布时间】:2019-11-01 23:48:44
【问题描述】:

我尝试使用 swagger 为发布请求创建 api,但无法确定我应该在 swagger 中配置哪些参数才能创建此 API。

我使用邮递员来获取有关响应的所有信息并大张旗鼓地实施。

'/api/v1/labels':
post:
      tags:
        - devices
      summary: 'create new label'
      description: 'create new label to a given device'
      operationId: createNewLabel
      produces:
        - application/json
      parameters:
        - name: x-access-token
          description: 'cognito token'
          in: header
          type: string
          required: true
        - in: body
          name: body
          description: add label details
          required: true
          schema:
            $ref: '#/definitions/labelRequest'
      responses:
        '201':
          schema:
            $ref: '#/definitions/labelRequest'
          description: Created

这是我写的定义:

labelRequest:
    type: object
    items:
      $ref: '#/definitions/labelRequestBody'

  labelRequestBody:
    type: object
    properties:
      device_ids:
        type: array
        items:
          type: string
          example: ["9bc11e25-4db2-4780-b761-390e3806082a"]
          format: uuid
      name:
        type: string
        example: new_label

这是 API 调用:

https:///api/v1/labels

带有这些标题:

x-access-token

并以这个主体作为有效负载:

{
    "device_ids":["ea4b9daa-07cd-4cd6-981f-c86e1e81f04c"],
    "name":"labelName"
}

预期结果是:201(已创建)

【问题讨论】:

    标签: java api swagger


    【解决方案1】:

    你快到了,只需将 labelRequest 架构更改为:

    definitions:
      labelRequest:
        type: object
        properties:
          device_ids:
            type: array
            items:
              type: string
              # Array item example should NOT be enclosed in []
              example: "9bc11e25-4db2-4780-b761-390e3806082a"
              format: uuid
          name:
            type: string
            example: new_label
    

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      相关资源
      最近更新 更多