gpt4 book ai didi

java - 需要 DropWizard 服务 Assets 帮助

转载 作者:搜寻专家 更新时间:2023-11-01 01:26:12 24 4
gpt4 key购买 nike

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

最佳答案

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

environment.getApplicationContext().setContextPath(applicationContextPath);

然后,AssetBundlerun()时被注册到这个applicationContext中(AssetBundle:109):

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

因此,assetbundle 始终在应用程序的 YAML 文件中设置的 applicationContextPath 内提供服务,因此在 此 applicationContextPath 之外提供它们不是可能(尽管文档这么说)

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

applicationContextPath: /

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

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

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

关于java - 需要 DropWizard 服务 Assets 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24822939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com