I just want to serve plain files in my Dropwizard app. That's all this does; let you serve files from your file system. It has about 0 extra features.
Add the dependency from Maven central.
Unverified (I use gradle these days)
<dependency>
<groupId>com.github.dirkraft.dropwizard</groupId>
<artifactId>dropwizard-file-assets</artifactId>
<version>0.0.2</version>
</dependency>
apply plugin: 'maven'
repositories {
mavenCentral()
}
dependencies {
compile 'com.github.dirkraft.dropwizard:dropwizard-file-assets:0.0.2'
}
This thing was mostly spurred on by development (I use IntelliJ for everything). I want to be able to refresh my browser rather than have to constantly restart dropwizard. I usually end up with two main methods: one for production and one for development. The right thing to do in production is serve static assets through nginx. The lazy thing to do is anything else, such as serving them directly from your java application.
Serving out of the classpath the traditional way with standard dropwizard-assets. Perhaps you have bundled your static assets in your jar.
@Override
public void initialize(Bootstrap<Configuration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/web", "/", "index.html"));
}
See a slightly more complete example in StandardAssetsBundleApp.java
Serving directly from the filesystem thanks to this small module, for instant browser refreshability.
@Override
public void initialize(Bootstrap<Configuration> bootstrap) {
// This is a relative path, relative to the working directory. If you run this main method in IntelliJ,
// by default the working directory is the project root directory.
bootstrap.addBundle(new FileAssetsBundle("src/test/resources/web", "/", "index.html"));
}
See a slightly more complete example in FileAssetsBundleApp.java
If you wanted, you could map it into your custom Configuration, but I don't usually care for that stuff until much later, if ever. If you do want that, you should check out dropwizard-configurable-assets-bundle. I just found it unwieldy to get started, but once you're going you might think about switching over to that for more of its features.
On startup if you have not restricted your logging too much, you might see a message from the FileAssetsBundle explaining what it's doing.
INFO [2015-01-20 03:27:32,380] com.github.dirkraft.dropwizard.fileassets.FileAssetsBundle: Registering FileAssetBundle with servlet name assets for request path /* from file system /Users/dirkraft/src/dropwizard-file-assets/src/test/resources/web
You can also enable debug level logging on com.github.dirkraft.dropwizard.fileassets.FileAssetServlet
, which will
then log the resolved absolute path to every resource request received, right before it attempts to read the file data.
dropwizard-file-assets is released under the Apache 2.0 license. It is a slight modification on the original dropwizard-assets module and so maintains dropwizard's licensing scheme.