我有最新的dropwizard設置。現在我創建了一個簡單的API,並且正在嘗試在頂部添加Swagger。 Dropwizard有一個Swagger實現,但示例代碼是針對需要addProvider和addResource的Yammer dropwizard 0.6.2的。 io.dropwizard環境似乎沒有這個功能。你可以讓我知道我可以做到這一點下io.dropwizard?如何使用swigger with dropwizard .0.7.0
3
A
回答
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"));
}
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
除了上面的@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());
}
相關問題
- 1. Azure Adal with Dropwizard
- 2. 使用帶有dropwizard的CORS頭過濾器0.7.0
- 3. 使用Dropwizard實現長輪詢服務器0.7.0
- 4. java.lang.NoClassDefFoundError:使用dropwizard
- 5. Jersey 2.7 with Dropwizard 0.7 - 是否支持?
- 6. 如何DropWizard
- 7. 如何使用Dropwizard測試HMAC認證?
- 8. 如何在Dropwizard中使用Jetty Continuations?
- 9. 如何在Netbeans項目中使用Dropwizard?
- 10. 使用openmdao-0.7.0安裝WISDEM的問題
- 11. dropwizard:使用log4j SocketAppender
- 12. Dropwizard banner.txt不使用
- 13. dplyr 0.7.0 tidyeval在包
- 14. Dropwizard如何工作?
- 15. 用於ORACLE 11g的Dropwizard遷移
- 16. 批註在Dropwizard
- 17. 在Dropwizard中使用緩存?
- 18. 如何在swigger php文檔中指定默認的json值?
- 19. 如何使用GROUP with DATEPART?
- 20. 如何使用VARIANT * with dynamicCall?
- 21. 如何使用f:form.select with onclick:
- 22. 如何使用slidingify with impress.js
- 23. 如何使用javascript:location.replace with response.redirect
- 24. 如何使用Parcelable with HashMap
- 25. 如何使用CSS with javascript
- 26. 如何使用webkit with scrapy
- 27. 如何將驗證器添加到dropwizard應用程序
- 28. 使用焊接與DropWizard
- 29. 客戶端和使用dropwizard
- 30. 在dropwizard中使用cometd
所以這得到我的JSON,這是偉大的,但我如何得到那個富有的swagger用戶界面? Theres顯而易見我在這裏失蹤。這是Drop Wizard文檔中的「靜態驢」討論嗎? –
這是否適合某人? – heaphach
dropwizard 1.0.0的任何更新? – nullpointer