问题描述
我想在使用 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楼
您可以使用responseContainer = "List"如下:
@ApiOperation(value = "retrieve items", response = Item.class, responseContainer = "List")
3楼
对于新包: io.swagger.v3.oas.annotations.responses.ApiResponse
您需要这样做(使用@ArraySchema
注释):
@ApiResponse(responseCode = "200", description = "",
content = {@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = Bar.class))
)}
)
4楼
只需将列表包装在一个对象中,如下所示:
public class ItemWrapper{
private List<Item> items;
}
然后将ItemWrapper
类作为API的响应:
@ApiResponse(code = 200, message = ApiResponseMessages.ITEM_FETCHED,
response = ItemWrapper.class)