18

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.

3 Answers 3

31

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" : ""
    }
]
Sign up to request clarification or add additional context in comments.

Comments

16

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

1 Comment

This should be the right answer
7

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

@ApiOperation(value = "retrieve items", response = Item.class, responseContainer = "List")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.