Dropwizard Assets not serving static content outside of root path

293 views
Skip to first unread message

Manny Rodriguez

unread,
Nov 26, 2015, 5:28:46 AM11/26/15
to dropwizard-user
Here's my Dropwizard (0.8.5) app's basic project structure:

    myapp/
        src/main/groovy/
            org/example/myapp/
                MyApp.groovy
                <lots of other packages/classes>
                controllers/
                    site/
                        SiteController.groovy
                    dashboard/
                        DashboardController.groovy
            org/example/myapp/views
                site/
                    SiteView.groovy
                dashboard/
                    DashboardView.groovy
        src/main/resources/
            assets/
                images/
                    mylogo.png
            org/example/myapp/views/
                site/
                    header.ftl
                    index.ftl
                dashboard/
                    dashboard.ftl

Where the gist of each of those classes is:

    class MyApp extends Application<MyAppConfiguration> {
        @Override
        void initialize(Bootstrap<MyAppConfiguration> bootstrap) {
            bootstrap.addBundle(new AssetsBundle('/assets/images', '/images', null, 'images'))
            bootstrap.addBundle(new ViewBundle())
        }

        // etc...
    }

    @Path('/')
    @Produces('text/html')
    class SiteController {
        @GET
        SiteView homepage() {
            new SiteView()
        }
    }

    @Path('/app/dashboard')
    @Produces('text/html')
    class DashboardController {
        @GET
        DashboardView dashboard() {
            new DashboardView()
        }
    }

    header.ftl  (dropwizard-views-freemarker)
    =========================================
    <!DOCTYPE html>
    <html>
        <head> <!-- lots of stuff omitted here for brevity --> </head>
        <body>
            <div class="well">
                <img src="images/mylogo.png" />
                <br/>This is the header!
            </div>

    index.ftl
    =========
    <#include "header.ftl">
            <p>
                Homepage!
            </p>
        </body>
    </html>

    dashboard.ftl
    =============
    <#include "../site/header.ftl">
            <p>
                Dashboard!
            </p>
        </body>
    </html>

So you can see I'm using DW as an actual web app/UI, and that I'm utilizing both Dropwizard Views (the Freemarker binding) as well as Dropwizard Assets.

When I run this, the app starts up just fine and I am able to visit both my homepage (served from '/' which maps to 'index.ftl') as well as my dashboard page (served from '/app/dashboard' which maps to 'dashboard.ftl').

The problem is that both pages use the 'header.ftl', which pulls in my 'assets/images/mylogo.png', but only my homepage actually renders the logo. On my dashboard page, I do see the "This is the header!" message, so I know the header is being resolved and included with my dashboard template. But, I get a failed-to-load-image "X" icon, and when I open my browser's dev tools I see that I'm getting HTTP 404s on the image.

So it seems that DW is unable to find my image asset from a view/URL not directly living under root ('/').

On the Dropwizard Assets page (link provided above) there's a peculiar warning:

"Either your application or your static assets can be served from the root path, but not both. The latter is useful when using Dropwizard to back a Javascript application. To enable it, move your application to a sub-URL."

I don't entirely understand what this means, but suspect it is the main culprit here. Either way, anyone see where I'm going awry, and what I can do (exact steps!) to fix this?

Manny Rodriguez

unread,
Nov 29, 2015, 9:55:14 AM11/29/15
to dropwizard-user
Here's the same question posted to StackOverflow with 150 rep bounty.

James Milligan

unread,
Nov 29, 2015, 10:07:38 AM11/29/15
to dropwizard-user
Load both pages up (the one that works, and the one that doesn't), and use Firebug or Chrome dev tools to inspect the logo element. What path is it trying to get to? I suspect on your index page it's going to http://some.thing/images/mylogo.png whereas on your dashboard it's trying to load http://some.thing/app/dashboard/images/mylogo.png

Try putting an additional / in front of the path in your template, and it should resolve from anywhere.

I couldn't comment on your SO post because I don't have the correct reputation yet, but will add this as an answer if it works.

Manny Rodriguez

unread,
Dec 2, 2015, 2:40:04 PM12/2/15
to dropwizard-user
Thanks James - this works! If you can put the explanation in an answer, I'll happily give you the green check + bounty! Thanks again!

James Milligan

unread,
Dec 2, 2015, 2:43:23 PM12/2/15
to dropwizard-user
Brilliant - I've popped my answer on.
Reply all
Reply to author
Forward
0 new messages