如何在dropwizard .0.7.0中使用swagger

问题描述:

我有最新的dropwizard设置。现在,我创建了一个简单的API,并尝试在顶部添加Swagger。 dropwizard有一个Swagger实现,但是示例代码与Yammer dropwizard 0.6.2相对,后者需要addProvider和addResource。 io.dropwizard环境似乎没有此功能。

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?

对于Dropwizard 0.7.0,我可以像这样配置swagger

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

编辑

要使用Dropwizard运行 Swagger UI ,请克隆仓库并复制将 dist 目录放入 src / main / resources / swagger / (根据需要进行定制)。然后像这样添加资产包:

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