Search code examples
javahttpdropwizardassetbundle

How to serve multiple static files using dropwizard for each url?


I used,

    bootstrap.addBundle(new AssetsBundle("/assets", "/", "a.js"));
    bootstrap.addBundle(new MultiPartBundle());

but I need to serve a file say b.js on entering /b and c.js on entering /c.

How to do so?


Solution

  • You can map specific resources by specifying the resourcePath as the actual file. So, using the examples you have mentioned this is how you could achieve that:

    bootstrap.addBundle(new AssetsBundle("/assets/a.js", "/a", null, "a"));
    bootstrap.addBundle(new AssetsBundle("/assets/b.js", "/b", null, "b"));
    bootstrap.addBundle(new AssetsBundle("/assets/c.js", "/c", null, "c"));
    

    However, I think this is not a good practice and will become unmanageable over time, to keep a mapping of a URL per resource.

    UPDATE For a more maintainable list of mappings I would recommend using something like the Configurable Assets Bundle for Dropwizard, where multiple mappings can be described on the configuration file. I have created a simple DW project which uses this lib and replicates the behaviour described on the question.