Dropwizard:无法提供静态HTML [英] Dropwizard: Unable to serve static HTML

查看:177
本文介绍了Dropwizard:无法提供静态HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为Dropwizard中的根路径 /提供一个静态html页面。到目前为止,我只收到一个错误页面,指出 HTTP错误404访问/。原因:未找到。。

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".

我一直遵循针对1.2.2的Dropwizard文档如本教程此处,其中包含一些更改代码以使我的服务正常工作。我的.yml文件中的根路径为 / profile / v1 以允许getAll服务正常工作(当我第一次启动时,由于多个servlet映射到路径/ * .yml如下:

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"));
}

这是向球衣注册的:

environment.jersey().setUrlPattern("/*");

其中 / resources 是我保留我的静态资产的目录,位于 java 目录之外。

where /resources is the directory I'm keeping my static assets, outside of the java directory.

到目前为止,我已经能够在此设置中让我的服务正常运行。例如,当我转到localhost:8080 / profile / v1 / name / getAll时,我可以从数据库中检索所有名称,如果我转到localhost:8080 / profile / v1 / titles / getAll,则可以从数据库中获取所有标题。如果使用localhost:8080(带或不带 /),我只会得到一个404页面,说它找不到 /。从理论上讲,这应该非常简单,所以我不确定我还应该做什么。

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.

编辑:

当我转到/ profile / v1时,得到以下信息:

When I go to /profile/v1, I get the following:

{
code: 404,
message: "HTTP 404 Not Found",
}

我应该提到我不希望我的html在这里提供;我希望将其作为根目录使用,因为我的所有服务都使用/ profile / v1路径。

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.

推荐答案

对代码进行了几处修改后,它才能正常工作。

After couple of modifications to your code, got it to working condition.


  1. AssetBundle路径是从项目资源文件夹计算得出的。因此,添加相对于它的路径。此处资产目录位于 $ {Project Root} / src / main / resources 目录

bootstrap.addBundle(new AssetsBundle("/assets/", "/"));


  • 删除显式的Jersey注册表项。我相信这是从配置继承的。

  • Remove explicit Jersey registry entry. I believe this is inherited from configuration.

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


  • 您将需要包含 dropwizard资产 到项目的依赖项。

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

    刚刚使用静态资产创建了一个示例项目

    For reference, just created a sample project with static assets.

    这篇关于Dropwizard:无法提供静态HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆