5

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.

DJ2
  • 1,721
  • 3
  • 34
  • 74
  • How are you running Dropwizard? Gradle? Maven? IDEA? i suspect your problem is that `webapp` isn't part of any of your resource paths so DW can't see it. Try mounting it as an absolute path, if that works, it's a classpath issue – tddmonkey Apr 30 '18 at 17:56
  • I'm running with maven. `webapp` is simply a sub-dir containing the static content. The file upload service itself has been registered in the environment configuration of my DW app. I'll see what providing the full absolute path does. @tddmonkey – DJ2 Apr 30 '18 at 18:45
  • I opted to move it under the conventional structure that dropwizard has. Still can't get the .html file to show up at the desired endpoint – DJ2 Apr 30 '18 at 19:28

1 Answers1

5

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.

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Isuru
  • 700
  • 9
  • 22