【问题标题】:Set List of Objects in Swagger API response在 Swagger API 响应中设置对象列表
【发布时间】:2019-02-21 10:44:20
【问题描述】:

我想使用 Swagger 在 API 的响应中发送对象列表。

@ApiResponse(code = 200, message = ApiResponseMessages.ITEM_FETCHED, 
response = "")

我有课 -

class Item{
   int id;
   String item_name;
}

我想要这样的回应 -

{
    {
       "id" : 0,
       "item_name" : ""
    }
    {
       "id" : 0,
       "item_name" : ""
    }
    {
       "id" : 0,
       "item_name" : ""
    }
}

我该怎么做。任何帮助将不胜感激。

【问题讨论】:

    标签: java spring-boot swagger swagger-ui swagger-2.0


    【解决方案1】:

    您也可以像这样设置 ApiReponse

    @ApiResponse(code = 200, message = ApiResponseMessages.ITEM_FETCHED,
                 response = Item.class, responseContainer = "List"
                )
    

    它会返回:

    [
        {
           "id" : 0,
           "item_name" : ""
        },
        {
           "id" : 0,
           "item_name" : ""
        },
        {
           "id" : 0,
           "item_name" : ""
        }
    ]
    

    【讨论】:

      【解决方案2】:

      对于新包:io.swagger.v3.oas.annotations.responses.ApiResponse

      你需要这样做(带有@ArraySchema注解):

      @ApiResponse(responseCode = "200", description = "",
                  content = {@Content(
                      mediaType = "application/json",
                      array = @ArraySchema(schema = @Schema(implementation = Bar.class))
                  )}
      )
      

      【讨论】:

      • 这应该是正确的答案
      【解决方案3】:

      您可以使用responseContainer = "List",如下:

      @ApiOperation(value = "retrieve items", response = Item.class, responseContainer = "List")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-04
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        • 1970-01-01
        相关资源
        最近更新 更多