如何在 swagger-editor 中将自定义对象表示为 api 的参数

How to represent custom object as parameter of an api in swagger-editor

我想在 swagger-editor 中将自定义对象表示为 api 的参数。假设我们正在调用 api /postInfo/data

@RequestMapping(value = "/postInfo/data", method = RequestMethod.POST)
public Info requestProcessing(@RequestBody Info info)
{
    // Implementation
}

以上方法包含信息模型 class 作为参数:

class Info
{
    private String id;
    private String name;
    private String desc;
}

如何在 swagger 编辑器中将其表示为 yaml 文档?

在 Swagger 2.0 中,可以在全局 definitions 部分定义对象模式:

definitions:
  Info:
    type: object
    properties:
      id:
        type: string
      name:
        type: string
      desc:
        type: string

然后 $ref 来自规范的其他部分。

如果将对象用作操作的输入,则该操作需要使用与该对象的架构相对应的 schema 定义正文参数。

paths:
  /postInfo/data:
    post:
      consumes:
        - application/json
      parameters:
        - in: body
          name: body
          required: true
          schema:
            $ref: '#/definitions/Info'
      responses:
        200:
          description: OK