【问题标题】:DropWizard Serving Assets help needed需要 DropWizard 服务资产帮助
【发布时间】:2014-07-18 10:32:23
【问题描述】:

帮助!我已经尝试了几个小时,在谷歌上搜索我能想到的任何东西。我有一个问题,我想在我的网站上显示我的静态内容而不是我的应用程序。 我修改了一个简单的 hello-world 应用程序:

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

这是我的 hello-world.yml:

server:
  type: simple
  applicationContextPath: /application/hello-world

template: Hello, %s!
defaultName: Stranger

我应用了 DropWizard 文档 (http://dropwizard.readthedocs.org/en/latest/manual/core.html#serving-assets) 所说的一切。但我就是无法访问 index.html

【问题讨论】:

    标签: java yaml pom.xml dropwizard


    【解决方案1】:

    我还没有看到一个实际的例子来证明记录的方式确实有效。 在查看 Dropwizard 源代码时,我得出结论,这实际上是不可能的:Jetty 应用程序上下文是由 SimpleServerFactory:103 中的配置参数 applicationContextPath 设置的:

    environment.getApplicationContext().setContextPath(applicationContextPath);
    

    然后,AssetBundles 在run() (AssetBundle:109) 上注册到此 applicationContext:

    environment.servlets().addServlet(assetsName, createServlet()).addMapping(uriPath + '*');
    

    因此,assetbundle 始终在应用程序的 YAML 文件中设置的applicationContextPath提供,因此无法在此 applicationContextPath 外提供它们(尽管文档这么说)

    实现此功能的更好方法是将应用程序配置为使用/ 路径:

    applicationContextPath: /
    

    然后,在您的应用程序代码中,在 bootstrap()run() 方法中,显式覆盖 Jersey 资源的路径并根据自己的喜好添加 AssetBundle:

    bootstrap.addBundle(new AssetsBundle("/static", "/"));
    
    environment.jersey().setUrlPattern("/application/*");
    

    【讨论】:

    【解决方案2】:

    我通过使用 AssetsBundle() 类的默认构造函数让它工作。

    使用默认构造函数,您的资源将在 java 类路径的目录中查找,例如

    /src/main/resources/assets/
    

    你必须只命名你的 applicationContextPath /application

    将浏览器指向静态内容的以下位置

    localhost:8080/application/assets/index.htm
    

    【讨论】:

    • 哇,非常感谢,终于有人解决了这个谜题。
    • 我得出的结论是,原始所需的设置因此是不可能的:从“/”路径提供静态包并从子路径提供 Jersey 应用程序。
    【解决方案3】:

    对于 Dropwizard 0.8.0 及更高版本,这是通过以下配置实现的:

    applicationContextPath: /
    rootPath: /application
    

    其中 applicationContextPath 是 Jetty 的 Context 路径,rootPath 是 Jersey 的。

    【讨论】:

      【解决方案4】:

      正如 Geert 所提到的,资产包需要从 applicationContextPath 中提供。但是,如果在 bootstrap 方法中添加 AssetsBundle,并在 run 方法中设置 contextPath,则 AssetServlet 会在 contextPath 设置后添加。

      我的解决方法是避免使用 AssetsBundle 并直接在 run 方法中添加 AssetsServlet(在设置 contextPath 之后):

      environment.getApplicationContext().setContextPath("/");
      environment.servlets().addServlet("assets", new AssetServlet("/assets", "/", "index.html", StandardCharsets.UTF_8)).addMapping("/*");
      

      【讨论】:

        猜你喜欢
        • 2012-03-08
        • 2015-06-16
        • 2021-01-06
        • 1970-01-01
        • 1970-01-01
        • 2016-03-12
        • 2021-09-06
        • 2012-03-11
        • 2018-07-31
        相关资源
        最近更新 更多