Set List of Objects in Swagger API response

别等时光非礼了梦想. 提交于 2019-12-11 15:37:38

问题


I want to send a list of objects in the response of an API using Swagger.

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

I have a class -

class Item{
   int id;
   String item_name;
}

I want a response like -

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

How can i do this. Any help would be appreciated.


回答1:


You also can set a ApiReponse like this:

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

It's will return:

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



回答2:


Just wrap the list in one object like this :

public class ItemWrapper{
private List<Item> items;
}

And Put the class ItemWrapperas response of the API :

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


来源:https://stackoverflow.com/questions/54805169/set-list-of-objects-in-swagger-api-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!