2014-04-25 63 views
3

我有最新的dropwizard設置。現在我創建了一個簡單的API,並且正在嘗試在頂部添加Swagger。 Dropwizard有一個Swagger實現,但示例代碼是針對需要addProvider和addResource的Yammer dropwizard 0.6.2的。 io.dropwizard環境似乎沒有這個功能。你可以讓我知道我可以做到這一點下io.dropwizard?如何使用swigger with dropwizard .0.7.0

回答

9

對於Dropwizard 0.7.0配置我招搖這樣的:

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/(定製視需要)。然後添加資產包是這樣的:

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

所以這得到我的JSON,這是偉大的,但我如何得到那個富有的swagger用戶界面? Theres顯而易見我在這裏失蹤。這是Drop Wizard文檔中的「靜態驢」討論嗎? –

+0

這是否適合某人? – heaphach

+0

dropwizard 1.0.0的任何更新? – nullpointer

2

有一些變化對揚鞭規範2.0,你可以在這裏看到:

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

即配置略有不同:

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

我可以從哪裏導入beanconfig? – Rentonie

+0

查看'io.swagger.jaxrs.config.BeanConfig' – fehguy

0

除了上面的@fehguy消息之外,我還想補充如何實際獲得dropwizard中的基本路徑(至少爲0.8),因爲顯然,你將無法從YML這樣得到基本路徑:

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

你將不得不做做這樣的事情:

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

參考GitHub的問題here這花了我天時間弄清楚。希望這可以幫助其他人

1

Dropwizard 1.0.2,Swagger 1.5.0。從您的應用程序的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()); 
}