在 Swagger API 响应中设置对象列表

我想使用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" : ""
    }
}

我怎么能这样做。任何帮助将不胜感激。


答案 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))
            )}
)

推荐