Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static assets with Dropwizard

Dropwizard version 1.3.0-rc6

Most documentation regarding serving static content are for older versions, and even the updated docs in Dropwizard Manual aren't exactly working for me.

I want to serve a static html file. I have modified the structure/paths of where these assets are served from, but can't quite get the configuration right in my environment.

Static content is located under the following structure


src/main/
├── java
│   └── org
│       └── com
│           └── query
│       
│               ├── rest
│               │   ├── api
│               │   ├── cli
│               │   ├── core
│               │   ├── db
│               │   ├── health
│               │   ├── resources
│               │   ├── tasks
│               │   └── views
│    
├── resources
│   ├── META-INF
│   │   ├── bin-license-notice
│   │   │   └── licenses
│   │   └── services
│   └── rest
│       ├── ftl
│       └── mustache
└── webapp
    ├── WEB-INF
    │   └── views
    │       └── jsp
    └── resources
        └── core
            ├── css
            └── js

multiFileUpload.html is inside src/main/webapp/resources/core dir, which is ultimately what I want to serve. However, this is different than dropwizard standards of src/main/resources/assets/.

I need the extended constructor to specify the individual AssetBundles as I plan on having multiple instances of AssetBundle. This is what I have inside my application initialize method.

@Override public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) { bootstrap.addBundle(new AssetsBundle("/webapp/resources/core/*", "/", null, "/MultiFileUpload.html")); }

I have also set a urlPattern in the applications run method. environment.jersey().setUrlPattern("/rest/*");

The root path in my config.yml is rootPath:/rest/*

I have an endpoint that the .html file should be served at. localhost:port/rest/upload/multiFile

I'm almost certain one of these paths is incorrect but I have tried to change them according to documentation examples, but haven't had any luck.

like image 636
DJ2 Avatar asked Aug 03 '26 23:08

DJ2


1 Answers

I just posted an answer to a similar question. I will list them here as well,

  1. AssetBundle path is calculated from project resources folder. Therefore add path relative to that. Here assets directory is located in ${Project Root}/src/main/resources directory

    bootstrap.addBundle(new AssetsBundle("/assets/", "/"));
    
  2. Remove explicit Jersey registry entry. I believe this is inherited from configuration.

    environment.jersey().setUrlPattern("/*"); /*this line should be removed*/
    

You will need to included dropwizard-assets to your project's dependencies.

For easy reference I created a sample project with static assets.

like image 84
Isuru Avatar answered Aug 05 '26 11:08

Isuru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!