Search code examples
swagger-uiswagger-2.0

Swagger examples are not shown on UI


Unable to get example field values in swagger UI.

@ApiOperation(value = "Sample API to test swagger doc", response = SampleResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sampleResponse",
                    dataType = "SampleResponse",
                    examples = @io.swagger.annotations.Example(
                            value = {
                                    @ExampleProperty(value = "{'code': '123', 'msg':'message'}", mediaType = "application/json")
                            }))
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, response = SampleResponse.class, message = "Success", examples = @io.swagger.annotations.Example(
                    value = {
                            @ExampleProperty(value = "{'code': '123', 'message':'success'}", mediaType = "*/*")
                    })),
            @ApiResponse(code = 401, message = "UnAuthorized"),
            @ApiResponse(code = 400, message = "Invalid ID supplied", response = ApiResponse.class,
                    examples = @Example(value =
                            {
                                    @ExampleProperty(mediaType = "application/json", value = "{\"code\" : \"42\", \"message\" : \"Invalid ID supplied\"}")
                            }
                    ))
    })
    @PosMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
    public SampleResponse get(@ApiParam(required = true) @RequestBody SampleResponse sampleResponse) {

pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

Example defined with annotation @ExampleProperty are missing in UI. In below image swagger UI example fields are shown with value as its default data type. enter image description here


Solution

  • I couldn't find much in swagger2, but upgraded to openapi, where this can be achieved by using @io.swagger.v3.oas.annotations.parameters.RequestBody for defining request body and its examples and @io.swagger.v3.oas.annotations.responses.ApiResponse for response.