我想使用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" : ""
}
}
我怎样才能做到这一点。任何帮助,将不胜感激。
只需将列表包装在一个对象中,如下所示:
public class ItemWrapper{
private List<Item> items;
}
并把API的类ItemWrapper
as响应:
@ApiResponse(code = 200, message = ApiResponseMessages.ITEM_FETCHED,
response = ItemWrapper.class)
你也可以像这样设置一个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" : ""
}
]