首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Spring中使用Swagger描述特定对象的列表?

如何在Spring中使用Swagger描述特定对象的列表?
EN

Stack Overflow用户
提问于 2021-12-17 12:26:13
回答 2查看 999关注 0票数 0

我使用Swagger描述了我的控制器,但是当我试图提取控制器的.yaml描述时,作为端点的响应,我找到了对象列表。如何使Swagger将这些列表描述为特定对象的列表,如汽车列表、房屋列表、动物列表等,然后描述什么是汽车、房子或动物等特定对象。我的案例是:

代码语言:javascript
复制
/dummy_endpoint:
get:
  tags:
    - foo-controller
  summary: Get foo list
  description: Send GET request to obtain foo list
  operationId: findAllFooUsingGET
  produces:
     - application/json
  responses:
    '200':
      description: Foo list obtained successfully
      schema:
        type: array
        items:
          type: object
    '401':
      description: Unauthorized
    '403':
      description: Forbidden
    '404':
      description: Not Found

我想要的是:

代码语言:javascript
复制
/dummy_endpoint:
  get:
    tags:
      - foo-controller
    summary: Get foo list
    description: Send GET request to obtain foo list
    operationId: findAllFooUsingGET
    produces:
      - application/json
    responses:
      '200':
        description: Foo list obtained successfully
        schema:
          type: array
          items:
            type: Foo
      '401':
        description: Unauthorized
      '403':
        description: Forbidden
      '404':
        description: Not Found
definitions:
  Foo:
    type: object
    properties:
      id:
        type: integer
        format: int32
      name:
        type: String
EN

回答 2

Stack Overflow用户

发布于 2021-12-17 12:38:55

我假设OpenAPI版本2.0 (基于示例中的语法)。如果你使用3.0,请告诉我。

你要找的是ref。在区段输入和输出模型中签入自夸规范,在区段数组中检入这里和数组中的多值参数。

例如:

代码语言:javascript
复制
...
responses:
  '200':
    description: Foo list obtained successfully
    schema:
      type: array
      items:
        $ref: "#/definitions/Foo"
...
definitions:
  Foo:
    type: object
    properties:
      ...
票数 1
EN

Stack Overflow用户

发布于 2021-12-20 10:53:27

解决我问题的是使用@ResponseApi注释中的responseContainer属性,其中我将类型的响应容器(如List、Array等)放入到存储在容器中的对象的响应属性类型中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70393165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档