17

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.

Mohd. Samar
  • 356
  • 1
  • 4
  • 11

3 Answers3

29

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" : ""
    }
]
Daniel Asanome
  • 478
  • 5
  • 7
14

For the new package: io.swagger.v3.oas.annotations.responses.ApiResponse

You need to do this (with @ArraySchema annotation):

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

You can use of responseContainer = "List" as below:

@ApiOperation(value = "retrieve items", response = Item.class, responseContainer = "List")
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Shashank
  • 709
  • 8
  • 16