DropWizard可以从jar文件外部提供资源吗?

zsl*_*ton 15 static-content dropwizard

在查看文档时,DropWizard似乎只能提供src/main/resources中的静态内容.我想将我的静态文件保存在jar文件之外的单独目录中.那可能吗?或者大多数人使用nginx/Apache作为他们的静态内容?

Lio*_*orH 11

是的,它可以,使用这个插件 - https://github.com/bazaarvoice/dropwizard-configurable-assets-bundle

  • 你不需要任何插件.只需[在类路径中添加文档根目录](http://stackoverflow.com/a/22221194/453605). (4认同)

cra*_*ack 7

根据Marcello Nuccio的回答,我仍然花了很多时间才能做到正确,所以这就是我所做的更详细的一点.

假设我有这个目录结构:

  • 我-dropwizard-的server.jar
  • staticdocs
    • 资产
      • image.png

那么这就是你要做的工作:

1)在dropwizard Application类中,添加一个新的AssetsBundle.如果您希望从其他URL提供资源,请更改第二个参数.

@Override
public void initialize(Bootstrap<AppConfiguration> bootstrap) {
    bootstrap.addBundle(new AssetsBundle("/assets/", "/assets/"));       
}
Run Code Online (Sandbox Code Playgroud)

2)通过配置像这样的maven-jar-plugin将文档根添加到类路径中.(以正确的形式获取"./staticdocs/"花了我一段时间.类路径是不可原谅的.)

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.4</version>
  <configuration>
    <archive>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addClasspath>true</addClasspath>
      </manifest>
      <manifestEntries>
        <Class-Path>./staticdocs/</Class-Path>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

3)这一步完全是可选的.如果要从不同的根路径(例如"app")提供Jersey REST资源,请将以下内容添加到配置YML中:

server:
  rootPath: /app/*
Run Code Online (Sandbox Code Playgroud)

现在您可以像这样访问静态内容,例如:

localhost:8080/assets/image.png
Run Code Online (Sandbox Code Playgroud)

  • 但那会把资产打包到罐子里,不是吗?这个问题的重点是从jar的OUTSIDE提供资产. (3认同)