问题
I'm currently working on serving a static html page in Dropwizard at the root path "/". So far, I've only received an error page stating "HTTP ERROR 404 Problem accessing /. Reason: Not Found".
I've followed the Dropwizard documentation for 1.2.2 in doing this, as well as this tutorial here, with some changes to the code for my services to work. My root path in my .yml file is /profile/v1
to allow my getAll services to work (when I first started, I was getting an error for having Multiple servlets map to path /*
. The .yml looks like this:
server:
type: simple
applicationContextPath: /
rootPath: /profile/v1
In addition, my initialize in the main application class is:
@Override
public void initialize(final Bootstrap<AutomationConfigServiceConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/../resources", "/", "index.html"));
}
This is registered to jersey as:
environment.jersey().setUrlPattern("/*");
where /resources
is the directory I'm keeping my static assets, outside of the java
directory.
So far, I've been able to get my services to work fine in this setup. For example, when I go to localhost:8080/profile/v1/name/getAll, I'm able to retrieve all the names from the database, and if I go to localhost:8080/profile/v1/titles/getAll, I get all titles from the database. If I use localhost:8080, with or without a "/", I just get a 404 page, saying it cannot find "/". In theory, this should be very simple, so I'm unsure what else I should do.
Edit:
When I go to /profile/v1, I get the following:
{
code: 404,
message: "HTTP 404 Not Found",
}
I should mention that I don't want my html to be served here; I'd like for it to be served at root, as the path /profile/v1 is used by all my services. This was requested to help set up DNS.
回答1:
After couple of modifications to your code, got it to working condition.
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
directorybootstrap.addBundle(new AssetsBundle("/assets/", "/"));
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 reference, just created a sample project with static assets.
来源:https://stackoverflow.com/questions/52500362/dropwizard-unable-to-serve-static-html