TSGames
TSGames

Reputation: 679

Swagger @ApiResponses as constant value

I use swagger to create a RESTful API, and have several endpoints which return the same errors and responses:

 @GET
    @Path("/some/endpoint")    
    @ApiOperation(
        value = "Some method", 
        notes = "Some method")
    @ApiResponses(
        value = { 
            @ApiResponse(code = 200, message = RestConstants.HTTP_200, response = Response.class),
            @ApiResponse(code = 400, message = RestConstants.HTTP_400, response = Error.class),        
            @ApiResponse(code = 401, message = RestConstants.HTTP_401, response = Error.class),        
            @ApiResponse(code = 403, message = RestConstants.HTTP_403, response = Error.class),        
            @ApiResponse(code = 404, message = RestConstants.HTTP_404, response = Error.class), 
            @ApiResponse(code = 500, message = RestConstants.HTTP_500, response = Error.class) 
        })    
    public Response someMethod(){...}

The amount of @ApiResonses is may about to change. As of now, I need to declare all of theses for my individual endpoint methods. Is there a way to use a constant value as an @ApiResponses value, e.g. like:

@ApiResponses(value = MY_RESPONSES)

Am I missing something?

Upvotes: 2

Views: 2044

Answers (2)

TheRealIndianBoi
TheRealIndianBoi

Reputation: 21

There is a way to do that: If your Problem is, that 'All' endpoints give same API-Responses, you can annotate the whole controller with:

@ApiResponses(value = {@ApiResponse(...), @ApiResponse(...)...})<br>
public class Controller{...}

And if you have a endpoint, that has another APIResponse, just add it to @Operation(response={here})

Upvotes: 0

Joseph Earl
Joseph Earl

Reputation: 23432

This unfortunately isn't possible using the Swagger annotations. For this to work ApiResponse would have to be a normal class/interface rather than an annotation.

Upvotes: 1

Related Questions