在Dropwizard 0.7.1中从基本URL提供静态内容



使用Dropwizard 0.6.2很容易,但随着迁移到0.7.x,它变得更加困难。我可以让它发挥作用,但不是以完全合适的方式。我希望我的RESTful API在"/API/*"点可用,静态内容从根URL"/"可用。

我目前能用0.7.1实现的最好效果是提供"/API/"中的API内容,以及"/API/assets/"的静态内容。这并不可怕,但如果我能实现最初设定的目标,事情会变得更好。我尝试了各种配置AssetBundle()的方法,并尝试使用https://github.com/bazaarvoice/dropwizard-configurable-assets-bundle,尝试使用我在源代码中看到的内容,并创建自己的专用StaticAssetsBundle类,但都无济于事。

我现在正在做的工作(它设法从"/api/assets/*"提供我的静态内容)如下:

public void initialize(Bootstrap<ConfigConfiguration> bootstrap) { bootstrap.addBundle(new AssetsBundle()); }

在我的配置文件中,我有。。。

server: type: simple connector: type: http port: 8458 applicationContextPath: /api .....

有人能给我一个简洁而完整的示例,说明如何在保持API在"/API/*"的服务的同时,实现从"/"提供静态内容?我搜索了很多遍,发现了一些提示、部分答案、在0.7.1中似乎不起作用的答案,我准备放弃,只从一个完全独立的服务器实例中提供我的静态内容(这可能是德国之声的人认为我无论如何都应该做的)。

Application:中使用这样的东西怎么样

@Override
public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
  // Static assets are in src/main/resources/assets
  bootstrap.addBundle(new AssetsBundle("/assets", "/"));
}
@Override
public void run(ExampleConfiguration configuration, Environment environment) throws Exception {
  environment.jersey().setUrlPattern("/api/*");
}

最新更新