How to use swagger with dropwizard .0.7.0

柔情痞子 提交于 2019-12-12 08:16:47

问题


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?


回答1:


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"));
}



回答2:


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);
  }



回答3:


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());
}



回答4:


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



来源:https://stackoverflow.com/questions/23301054/how-to-use-swagger-with-dropwizard-0-7-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!