使用 Dropwizard 提供静态资源

Serving static assets with Dropwizard

Dropwizard 版本 1.3.0-rc6

大多数关于提供静态内容的文档都是针对旧版本的,甚至 Dropwizard Manual 中的更新文档也不完全适合我。

我想提供静态 html 文件。我已经修改了提供这些资产的位置的 structure/paths,但无法在我的环境中完全正确地配置。

静态内容位于以下结构下


src/main/
├── java
│   └── org
│       └── com
│           └── query
│       
│               ├── rest
│               │   ├── api
│               │   ├── cli
│               │   ├── core
│               │   ├── db
│               │   ├── health
│               │   ├── resources
│               │   ├── tasks
│               │   └── views
│    
├── resources
│   ├── META-INF
│   │   ├── bin-license-notice
│   │   │   └── licenses
│   │   └── services
│   └── rest
│       ├── ftl
│       └── mustache
└── webapp
    ├── WEB-INF
    │   └── views
    │       └── jsp
    └── resources
        └── core
            ├── css
            └── js

multiFileUpload.htmlsrc/main/webapp/resources/core 目录中,这最终是我想要服务的。但是,这与 src/main/resources/assets/.

的 dropwizard 标准不同

我需要扩展构造函数来指定单个 AssetBundle,因为我计划拥有 AssetBundle 的多个实例。这就是我的应用程序初始化方法中的内容。

@Override public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) { bootstrap.addBundle(new AssetsBundle("/webapp/resources/core/*", "/", null, "/MultiFileUpload.html")); }

我还在应用程序 运行 方法中设置了一个 urlPattern。 environment.jersey().setUrlPattern("/rest/*");

我的config.yml中的根路径是rootPath:/rest/*

我有一个端点,应该在该端点提供 .html 文件。 localhost:port/rest/upload/multiFile

我几乎可以肯定这些路径之一是不正确的,但我已尝试根据文档示例进行更改,但没有成功。

我刚刚针对类似问题发布了 。我也会在这里列出它们,

  1. AssetBundle 路径是根据项目资源文件夹计算的。因此添加相对于它的路径。这里assets目录位于${Project Root}/src/main/resources目录

    bootstrap.addBundle(new AssetsBundle("/assets/", "/"));
    
  2. 删除明确的 Jersey 注册表项。我相信这是从配置继承的。

    environment.jersey().setUrlPattern("/*"); /*this line should be removed*/
    

您需要将 dropwizard-assets 添加到项目的依赖项中。

为了便于参考,我创建了一个包含静态资产的 sample project