Adding swagger to your DropWizard application
Adding swagger to your dropwizard application is very easy. I followed the tutorial for information on how to do so, and here were the actual changes I made.
Step 1: Update your pom.xml file
<!-- Swagger UI --> <dependency> <groupId>com.wordnik</groupId> <artifactId>swagger-jaxrs_2.10</artifactId> <version>1.3.12</version> </dependency> <!-- CORS support for Swagger--> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlets</artifactId> <version>9.2.9.v20150224</version> </dependency>
Step 2: Update your DW application configuration
To make the configuration portable across environments, you can add the base path to your configuration file.
@Valid @NotNull @JsonProperty private String swaggerBasePath; public String getSwaggerBasePath(){ return swaggerBasePath; }</pre>
Step 3: Update the Application class
Add the swagger init to the run method of the Application Class.
// init Swagger resources initSwagger(configuration, environment);
And add the definition of that method to the same class.
private void initSwagger(ExampleServiceConfiguration configuration, Environment environment) { // Swagger Resource environment.jersey().register(new ApiListingResourceJSON()); // Swagger providers environment.jersey().register(new ApiDeclarationProvider()); environment.jersey().register(new ResourceListingProvider()); // Swagger Scanner, which finds all the resources for @Api Annotations ScannerFactory.setScanner(new DefaultJaxrsScanner()); // Add the reader, which scans the resources and extracts the resource information ClassReaders.setReader(new DefaultJaxrsApiReader()); // required CORS support FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORS", CrossOriginFilter.class); filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); filter.setInitParameter("allowedOrigins", "*"); // allowed origins comma separated filter.setInitParameter("allowedHeaders", "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin"); filter.setInitParameter("allowedMethods", "GET,PUT,POST,DELETE,OPTIONS,HEAD"); filter.setInitParameter("preflightMaxAge", "5184000"); // 2 months filter.setInitParameter("allowCredentials", "true"); // Set the swagger config options SwaggerConfig config = ConfigFactory.config(); config.setApiVersion("1.0.1"); config.setBasePath(configuration.getSwaggerBasePath()); }
Step 4: Update application yml file
swaggerBasePath: http://localhost:9080
Step 5: Annotate your classes
import com.codahale.metrics.annotation.Timed; import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation; import com.wordnik.swagger.annotations.ApiResponse; import com.wordnik.swagger.annotations.ApiResponses; import javax.ws.rs.GET; import javax.ws.rs.Path; /** * Created by mlynch */ @Path("/hello") @Api(value="/hello", description="Operations on the hello object") public class HelloResource { private final MessagesConfiguration conf; public HelloResource(MessagesConfiguration conf) { this.conf = conf; } @GET @Timed @ApiOperation(value="Return a simple greeting", notes="some day this will do more, it believes in a growth mentality.") @ApiResponses(value={ @ApiResponse(code=400, message="Invalid ID"), }) public String sayHello() { return conf.getHello(); } }
Full tutorial link here: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-JAX-RS-Project-Setup-1.5.X