Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否将dropwizard配置为(几乎)所有路由的服务器index.html?_Java_Configuration_Jetty_Dropwizard - Fatal编程技术网

Java 是否将dropwizard配置为(几乎)所有路由的服务器index.html?

Java 是否将dropwizard配置为(几乎)所有路由的服务器index.html?,java,configuration,jetty,dropwizard,Java,Configuration,Jetty,Dropwizard,我正在构建一个单页应用程序,它在客户端执行所有html请求路由,在后端使用dropwizard提供一系列JSON服务 基本上,我很难让dropwizard中的jetty为每个请求提供index.html,除了以下路径: /css /i18n /img /js /lib /services /templates 事实上,我很难找到告诉您如何设置http路由的文档。(我不是爪哇人) 以下是我的简单yaml配置:` http: port: 8082 adminPo

我正在构建一个单页应用程序,它在客户端执行所有html请求路由,在后端使用dropwizard提供一系列JSON服务

基本上,我很难让dropwizard中的jetty为每个请求提供index.html,除了以下路径:

  /css
  /i18n
  /img
  /js
  /lib
  /services
  /templates
事实上,我很难找到告诉您如何设置http路由的文档。(我不是爪哇人)

以下是我的简单yaml配置:`

http:
  port: 8082
  adminPort: 8083
  rootPath: /service/*`
我需要为acheive添加什么


谢谢

我这样做时没有更改配置。事实上,我只花了一行代码,就被放入了我的
应用程序的
初始化
方法中:

bootstrap.addBundle(new AssetsBundle("/app", "/", "index.html", "static"));
ErrorPageErrorHandler eph = new ErrorPageErrorHandler();
eph.addErrorPage(404, "/error/404");
environment.getApplicationContext().setErrorHandler(eph);
它基本上是说在我的JAR文件中,以URL模式
/
提供
/app
下的任何内容,默认文件为
index.html
。这个包将被命名为static,但是您可以选择任何您喜欢的名称


请注意,我使用的是Dropwizard的0.7.0-rc2版本,我不确定它是否也适用于早期版本。

@mthmulders answer在我转到“/”时对我有效,在页面未重新加载时对其他url有效,但在我在“/foo/bar/bash”或任何其他url刷新页面时无效。我通过映射任何404响应以返回index.html页面修复了这个问题

将静态资产添加到
应用程序的
初始化
方法
类:

bootstrap.addBundle(new AssetsBundle("/app", "/", "index.html", "static"));
将新404页面的映射添加到
应用程序
类的
运行
方法:

bootstrap.addBundle(new AssetsBundle("/app", "/", "index.html", "static"));
ErrorPageErrorHandler eph = new ErrorPageErrorHandler();
eph.addErrorPage(404, "/error/404");
environment.getApplicationContext().setErrorHandler(eph);
添加映射到
/error/404

@GET
@Path("/error/404")
@Produces(MediaType.TEXT_HTML)
public Response error404() {
        // get html file from resources here...
        return Response.status(Response.Status.OK)
                .entity("<html>foo</html>")
                .build();
}
@GET
@路径(“/error/404”)
@生成(MediaType.TEXT\u HTML)
公共响应错误404(){
//从这里的资源获取html文件。。。
返回Response.status(Response.status.OK)
.实体(“foo”)
.build();
}

完成此操作后,我能够在“/foo/bar/bash”刷新页面,它仍然返回index.html文件。

@jjbskir有正确的答案。还有一些其他的解决方案和,但它们引入了不必要的依赖关系

以下是为索引页提供服务的更完整资源:

@Path("/errorpage")
public class ErrorService {

    private static String indexPage;

    @GET
    @Produces(MediaType.TEXT_HTML)
    public Response get404() {

        if (indexPage == null) {
            InputStream in = this.getClass().getResourceAsStream("/assets/index.html");
            try (Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name())) {
                indexPage = scanner.useDelimiter("\\A").next();
            }
        }

        return Response.status(Response.Status.OK)
                .entity(indexPage)
                .build();
    }

}
它缓存索引页,因为它在运行时不应更改。如果在开发时使用此服务,您可能希望省略缓存

修改对ErrorPageErrorHandler的调用,如下所示:

eph.addErrorPage(404, "/api/errorpage");

如果api所在的位置是/api前缀,请不要忘记它,也不要忘记注册资源。

唉,我们只使用0.6.2,而此方法不可用,当它出现时,我们将升级到.7,不过我会尝试一下。谢谢你的回复。这在0.8.x中有变化吗?我正试图完成与OP相同的任务,在OP中,指向不指向实际资产的路径的所有请求都会得到index.html服务。如本文所述,添加AssetsBundle会导致请求“/”以获取index.html,但请求“/foo/bar/bash”以获取404。与v0.9.1中的@AwesomeTown存在相同的问题:(您如何从dropwizard的索引html加载react应用程序?