使用Dropwizard提供静态资源文件服务

5

Dropwizard版本1.3.0-rc6

关于提供静态内容的大多数文档都是针对旧版本的,即使在Dropwizard手册中更新的文档也不完全适用于我。

我想提供一个静态的html文件。我已经修改了这些资源所服务的结构/路径,但在我的环境中无法完全正确地配置。

静态内容位于以下结构下


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.html

multiFileUpload.html 位于src/main/webapp/resources/core目录下,这是我最终要提供的内容。然而,这与dropwizard标准的src/main/resources/assets/不同。

我需要扩展构造函数以指定单个AssetBundle,因为我计划有多个AssetBundle实例。这是我在应用程序初始化方法中的代码:
``` @Override public void initialize(Bootstrap 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 ```
我几乎可以确定其中一个路径不正确,但我已经尝试根据文档示例更改它们,但没有任何运气。

你是如何运行Dropwizard的?使用Gradle?Maven?IDEA?我怀疑你的问题在于webapp不是任何资源路径的一部分,因此DW无法看到它。尝试将其作为绝对路径挂载,如果可以工作,则是类路径问题。 - tddmonkey
我正在使用Maven运行。webapp只是包含静态内容的子目录。文件上传服务本身已在我的DW应用程序的环境配置中注册。我会看看提供完整的绝对路径会发生什么。@tddmonkey - DJ2
我选择将它移动到Dropwizard的传统结构下。仍然无法在所需的端点显示.html文件。 - DJ2
1个回答

5

我刚刚在一个相似的问题中发布了一个答案。在这里也会列出它们:

  1. AssetBundle path is calculated from project resources folder. Therefore add path relative to that. Here assets directory is located in ${Project Root}/src/main/resources directory

    bootstrap.addBundle(new AssetsBundle("/assets/", "/"));
    
  2. Remove explicit Jersey registry entry. I believe this is inherited from configuration.

    environment.jersey().setUrlPattern("/*"); /*this line should be removed*/
    
你需要将dropwizard-assets加入到项目的依赖中。
为了方便参考,我创建了一个包含静态资源的示例项目

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接