Swagger Annotation to add a json example to the response class

Chayma Atallah picture Chayma Atallah · Nov 3, 2017 · Viewed 6.9k times · Source

enter image description here

How can I insert a default value instead of the "{}" using swagger annotations?

Answer

Yogi picture Yogi · Nov 3, 2017

You need to annonatate your Rest method with @ApiResponses and @ApiResponse.

For e.g.

@ApiResponses(value = { 
                        @ApiResponse(code = 200, message = "Successful retrieval of demand",responseContainer="List"),
                        @ApiResponse(code = 404, message = "Demand does not exist", response = Your Error response.class),
                        @ApiResponse(code = 500, message = "Internal server error", response = Your Error response.class) 
                    }
            )

Annonatate your Json Response class with @ApiModel and its field with @ApiModelProperty like

@ApiModel(description = "Your comment")
public class Demand implements Serializable {

    private static final long serialVersionUID = 1L;


    @ApiModelProperty(notes = "Your comments", required = true, example = "example value")
    private String demandId;
}