gpt4 book ai didi

java - 如何使用 swagger 使用以 json 作为有效负载的 post 请求创建 api

转载 作者:行者123 更新时间:2023-12-02 09:45:53 25 4
gpt4 key购买 nike

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

我使用 postman 来获取有关响应的所有信息并在 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-访问 token

并以此正文作为有效负载:

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

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

最佳答案

您已经快完成了,只需将 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

关于java - 如何使用 swagger 使用以 json 作为有效负载的 post 请求创建 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56664258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com