Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can DropWizard serve assets from outside the jar file?

In looking at the documentation, it appears that DropWizard is only able to serve static content living in src/main/resources. I'd like to keep my static files in a separate directory outside the jar file. Is that possible? Or do most people use nginx/Apache for their static content?

like image 469
zslayton Avatar asked Jan 08 '13 20:01

zslayton


People also ask

Does Dropwizard use Log4j?

Dropwizard uses Logback for its logging backend. It provides an slf4j implementation, and even routes all java. util. logging , Log4j, and Apache Commons Logging usage through Logback.

Does Dropwizard use Jetty?

Jetty for HTTP Because you can't be a web application without HTTP, Dropwizard uses the Jetty HTTP library to embed an incredibly tuned HTTP server directly into your project.

What is Dropwizard framework?

What is Dropwizard? Dropwizard is an open source Java framework for developing ops-friendly, high-performance RESTful backends. It was developed by Yammer to power their JVM based backend. Dropwizard provides best-of-breed Java libraries into one embedded application package.


2 Answers

yes, it can, using this plugin - https://github.com/bazaarvoice/dropwizard-configurable-assets-bundle

like image 111
LiorH Avatar answered Oct 21 '22 15:10

LiorH


Working off of Marcello Nuccio's answer, it still took me the better part of my day to get it right, so here is what I did in a bit more detail.

Let's say I have this directory structure:

  • my-dropwizard-server.jar
  • staticdocs
    • assets
      • image.png

Then this is what you have to do to make it work:

1) In your dropwizard Application class, add a new AssetsBundle. If you want your assets to be served from a different URL, change the second parameter.

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

2) Add the document root to your classpath by configuring the maven-jar-plugin like this. (Getting the "./staticdocs/" in the correct form took me a while. Classpaths are unforgiving.)

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

3) This step is entirely optional. If you want to serve your Jersey REST Resources from a different root path (e.g. "app"), add the following to your configuration YML:

server:
  rootPath: /app/*

Now you can access your static content like this, for example:

localhost:8080/assets/image.png
like image 33
craddack Avatar answered Oct 21 '22 17:10

craddack