30 May 2016

Swagger with Dropwizard

In this post I will discuss how to integrate swagger in to dropwizard project.If you new to dropwizard this will help you to understand what is the dropwizard. So swagger is api documentation tool . For get basic knowledge swagger this will helps. But today I will integrate swagger in to dropwizard.

First you need to add this dependency to your project.

1
2
3
4
5
<dependency>
    <groupId>com.smoketurner</groupId>
    <artifactId>dropwizard-swagger</artifactId>
    <version>0.9.2-3</version>
</dependency>

And next thing is using annotation you have to document  resource class. This is the example of how to use swagger anotation.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Path("/studentManagement/api/")
@Api(value = "Student Management API", description = "Student Management API")
public interface StudentManagement {
    @GET
    @PermitAll
    @Path("/getAllStudent")
    @ApiOperation(value = "Get All Students")
    @ApiResponses(value = { @ApiResponse(code = 400, message = "Error Occurred 
                                                     while getting data"),
            @ApiResponse(code = 404, message = "Student not found"),
            @ApiResponse(code = 405, message = "Validation exception") })
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    Response getAllStudents();

    @GET
    @PermitAll
    @Path("/getStudent")
    @ApiOperation(value = "Get Students by Name")
    @ApiResponses(value = { @ApiResponse(code = 400, message = "Error Occurred 
                                                     while getting data"),
            @ApiResponse(code = 404, message = "Student not found"),
            @ApiResponse(code = 405, message = "Validation exception") })
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    Response getStudent(@QueryParam("name") String name,
                                 @QueryParam("age") int age);
}

And next step is you need add this swagger configuration to your dropwizard configuration class.


1
2
@JsonProperty("swagger")
    public SwaggerBundleConfiguration swaggerBundleConfiguration

And next step is in your dropwizard application you need to add swagger configuration bundle.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@Override
    public void initialize(Bootstrap bootstrap) {
        bootstrap.addBundle(new SwaggerBundle() {
            @Override
            protected SwaggerBundleConfiguration 
                  getSwaggerBundleConfiguration(StudentManagementServerConfiguration configuration) {
                return configuration.swaggerBundleConfiguration;
            }
        });
    }
Last step is need to add your resource class package to yaml configuration file.


1
swagger:   resourcePackage: dev.innova.student.api




And that is all for this blog post.If you have any issue please
comment here.Thanks

No comments:

Post a Comment