如何在dropwizard.0.7.0中使用swagger

如何在dropwizard.0.7.0中使用swagger,swagger,dropwizard,Swagger,Dropwizard,我有最新的dropwizard设置。现在我已经创建了一个简单的API,我正在尝试在上面添加Swagger。dropwizard有一个招摇过市的实现,但是示例代码是针对Yammer dropwizard 0.6.2的,它需要addProvider和addResource。io.dropwizard环境似乎没有此功能。您能告诉我如何在io.dropwizard下执行此操作吗?对于dropwizard 0.7.0,我将swagger配置为: DefaultServerFactory defaultSe

我有最新的dropwizard设置。现在我已经创建了一个简单的API,我正在尝试在上面添加Swagger。dropwizard有一个招摇过市的实现,但是示例代码是针对Yammer dropwizard 0.6.2的,它需要addProvider和addResource。io.dropwizard环境似乎没有此功能。您能告诉我如何在io.dropwizard下执行此操作吗?

对于dropwizard 0.7.0,我将swagger配置为:

DefaultServerFactory defaultServerFactory = (DefaultServerFactory) serverConfiguration.getServerFactory();
String basePath = defaultServerFactory.getApplicationContextPath();
void-configureSwagger(环境){
environment.jersey().register(new ApiListingResourceJSON());
environment.jersey().register(new-ApiDeclarationProvider());
environment.jersey().register(new ResourceListingProvider());
setScanner(新的defaultjaxrscanner());
setReader(新的DefaultJaxrsApiReader());
swagggerconfig=ConfigFactory.config();
config.setApiVersion(API_版本);
config.setBasePath(“..”+environment.getApplicationContext().getContextPath());
}
编辑

要使用Dropwizard运行,请克隆repo并将
dist
目录复制到
src/main/resources/swagger/
(根据需要进行自定义)。然后按如下方式添加资产包:

DefaultServerFactory defaultServerFactory = (DefaultServerFactory) serverConfiguration.getServerFactory();
String basePath = defaultServerFactory.getApplicationContextPath();
@覆盖
公共无效初始化(引导引导引导){
addBundle(新资产包(“/swagger/”、“/docs”、“index.html”);
}

您可以在此处看到Swagger spec 2.0的一些更改:

即配置略有不同:

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

除了上面@fehguy的消息之外,我还想补充一下如何在dropwizard中实际获取基本路径(至少是0.8),因为显然您无法从yml获取基本路径,如下所示:

config.setBasePath(".." + environment.getApplicationContext().getContextPath());
您必须执行以下操作:

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

请参阅github问题,我花了几天时间才弄清楚。希望这对其他人有所帮助

Dropwizard 1.0.2,Swagger 1.5.0。从应用程序的
run()
调用此函数


这让我得到了JSON,这很好,但我如何获得丰富的招摇过市UI呢?很明显,我在这里遗漏了一些东西。这是Drop Wizard docs中的“静态asses”讨论吗?@nullpointer有关在1.0.0中生成swagger.json的问题,请参见下面的答案,我可以从何处导入beanconfig?请查看
io.swagger.jaxrs.config.beanconfig