如何使用Swagger使用带有JSON作为有效载荷的后请求创建API

时间:2019-06-19 09:21:57

标签: java api swagger

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

我使用邮递员来获取有关响应的所有信息并大刀阔斧地实施。

'/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(已创建)

1 个答案:

答案 0 :(得分: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