Question

I have the latest dropwizard setup. Now I have created a simple API and I am trying to add Swagger on top. There is a Swagger implementation for dropwizard but the sample code is against Yammer dropwizard 0.6.2 which requires addProvider and addResource. The io.dropwizard environment doesn't seem to have this function. Can you please let me know how I can do this under io.dropwizard?

Was it helpful?

Solution

For Dropwizard 0.7.0 I configure swagger like this:

void configureSwagger(Environment environment) {
  environment.jersey().register(new ApiListingResourceJSON());
  environment.jersey().register(new ApiDeclarationProvider());
  environment.jersey().register(new ResourceListingProvider());
  ScannerFactory.setScanner(new DefaultJaxrsScanner());
  ClassReaders.setReader(new DefaultJaxrsApiReader());
  SwaggerConfig config = ConfigFactory.config();
  config.setApiVersion(API_VERSION);
  config.setBasePath(".." + environment.getApplicationContext().getContextPath());
}

EDIT

To run Swagger UI with Dropwizard clone the repo and copy the dist directory into src/main/resources/swagger/ (customizing as necessary). Then add the asset bundle like this:

@Override
public void initialize(Bootstrap<ApplicationConfiguration> bootstrap) {
  bootstrap.addBundle(new AssetsBundle("/swagger/", "/docs", "index.html"));
}

OTHER TIPS

There are some changes for Swagger spec 2.0 that you can see here:

https://github.com/swagger-api/swagger-core/tree/develop_2.0/samples/java-dropwizard

Namely the configuration is slightly different:

  @Override
  public void run(SwaggerSampleConfiguration configuration, Environment environment) {
    environment.jersey().register(new ApiListingResource());

    // specific to the sample
    environment.jersey().register(new PetResource());
    environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
    BeanConfig config = new BeanConfig();

    // api specific configuration
    config.setTitle("Swagger sample app");
    config.setVersion("1.0.0");
    config.setResourcePackage("com.wordnik.swagger.sample");
    config.setScan(true);
  }

Dropwizard 1.0.2, Swagger 1.5.0. Call this from your application's run().

private void configureSwagger(Environment environment) {
    BeanConfig magicBean = new BeanConfig();
    magicBean.setVersion("0.1");
    magicBean.setTitle("title");
    magicBean.setBasePath("/api");
    magicBean.setResourcePackage("com.me.resources");
    magicBean.setScan(true);
    environment.jersey().register(new ApiListingResourceJSON());
    environment.jersey().register(new SwaggerSerializers());
}

In addition to what @fehguy messaged above, I would like to add on how to actually get the base path in dropwizard as well (atleast with 0.8) because apparently you will not be able to get basePath from the yml like this :

config.setBasePath(".." + environment.getApplicationContext().getContextPath());

You will have to do do something like this:

DefaultServerFactory defaultServerFactory = (DefaultServerFactory) serverConfiguration.getServerFactory();
String basePath = defaultServerFactory.getApplicationContextPath();

Refer to the github issue here which took me DAYS to figure out. Hope this helps others

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top