Dropwizard: Unable to serve static HTML

你说的曾经没有我的故事 提交于 2019-12-25 01:44:58

问题


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.

  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 reference, just created a sample project with static assets.



来源:https://stackoverflow.com/questions/52500362/dropwizard-unable-to-serve-static-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!