【问题标题】:Dropwizard: Unable to serve static HTMLDropwizard:无法提供静态 HTML
【发布时间】:2019-03-01 04:57:34
【问题描述】:

我目前正在处理 Dropwizard 中根路径“/”的静态 html 页面。到目前为止,我只收到一个错误页面,说明“HTTP ERROR 404 Problem访问/。原因:未找到”。

我已经按照Dropwizard documentation for 1.2.2 以及本教程here 执行此操作,对代码进行了一些更改以使我的服务正常工作。我的 .yml 文件中的根路径是 /profile/v1 以允许我的 getAll 服务工作(当我第一次启动时,我收到了一个错误,因为我有 Multiple servlets map to path /*。.yml 看起来像这样:

server:
  type: simple
  applicationContextPath: /
  rootPath: /profile/v1

另外,我在主应用类中的初始化是:

@Override
public void initialize(final Bootstrap<AutomationConfigServiceConfiguration> bootstrap) {
    bootstrap.addBundle(new AssetsBundle("/../resources", "/", "index.html"));
}

这在球衣上注册为:

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

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

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

编辑:

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

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

我应该提到我不希望我的 html 在这里提供;我希望它在根目录下提供服务,因为我的所有服务都使用路径 /profile/v1。这是为了帮助设置 DNS。

【问题讨论】:

  • 据我了解,您的静态页面应该可以从localhost:8080/profile/v1获得
  • 这样做会产生 404 json 响应。我将编辑问题以包含此内容。

标签: java html static dropwizard serving


【解决方案1】:

在对您的代码进行几次修改后,使其处于工作状态。

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

    bootstrap.addBundle(new AssetsBundle("/assets/", "/"));
    
  2. 删除显式 Jersey 注册表项。我相信这是从配置中继承的。

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

您需要将dropwizard-assets 包含到项目的依赖项中。

作为参考,刚刚创建了一个带有静态资产的sample project

【讨论】:

    猜你喜欢
    • 2015-08-20
    • 2018-10-10
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2012-09-06
    相关资源
    最近更新 更多