Help! I've been trying for hours, googling anything I could think of. I have a problem, that I would like to show my static content instead of my application on my site. I modified a simple hello-world application:
public static void main(String[] args) throws Exception {
Class.forName("org.sqlite.JDBC");
new HelloWorldApplication().run(args);
}
@Override
public String getName() {
return "hello-world";
}
@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/assets/*", "/"));
}
@Override
public void run(HelloWorldConfiguration configuration, Environment environment) {
final HelloWorldResource resource = new HelloWorldResource(
configuration.getTemplate(),
configuration.getDefaultName()
);
final AddResource addResource = new AddResource();
final DeleteResource deleteResource = new DeleteResource();
final TemplateHealthCheck healthCheck = new TemplateHealthCheck(configuration.getTemplate());
environment.healthChecks().register("template", healthCheck);
environment.jersey().register(resource);
environment.jersey().register(addResource);
environment.jersey().register(deleteResource);
}
Here's my hello-world.yml:
server:
type: simple
applicationContextPath: /application/hello-world
template: Hello, %s!
defaultName: Stranger
I applied everything, what the DropWizard docs (http://dropwizard.readthedocs.org/en/latest/manual/core.html#serving-assets) said. But I just cannot manage to reach the index.html
I have not seen an actual example that proves that the documented way actually works.
And when looking in the Dropwizard source, I conclude that this is in fact not possible: the Jetty application context is set by the configuration parameter applicationContextPath
in SimpleServerFactory:103
:
environment.getApplicationContext().setContextPath(applicationContextPath);
And after that, the AssetBundle
s are registered into this applicationContext upon run()
(AssetBundle:109
):
environment.servlets().addServlet(assetsName, createServlet()).addMapping(uriPath + '*');
So, assetbundles are always served within the applicationContextPath
that is set in the application's YAML file, so serving them outside this applicationContextPath is not possible (despite the docs saying so)
A better way to get this working, is to configure the application to use the /
path:
applicationContextPath: /
And then, in your application's code, in the bootstrap()
and run()
methods, explicitly override the path for Jersey resources and add AssetBundles to your liking:
bootstrap.addBundle(new AssetsBundle("/static", "/"));
environment.jersey().setUrlPattern("/application/*");
I got it working by using the default constructor for the AssetsBundle() class.
With the default constructor your resources will gets looked up in a directory on the java classpath e.g.
/src/main/resources/assets/
and your have to name your applicationContextPath only /application
Point your browser to the folling location for static content
localhost:8080/application/assets/index.htm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With