使用Akka-http Java加载静态html文件 [英] Loading static html files using Akka-http Java

查看:198
本文介绍了使用Akka-http Java加载静态html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力从一个简单的aka-http Java服务器中加载静态文件。我尝试了多种选择,但始终会遇到以下错误:

I've been struggling to get a static file to load from a simple aka-http Java server. I've tried a number of options, but I always get the following error:

[ERROR] [09/07/2018 11:52:06.258] [AkkaRestApiApp-akka.actor.default-dispatcher-4] [akka.actor.ActorSystemImpl(AkkaRestApiApp)] Error during processing of request: 'java.lang.NullPointerException (No error message supplied)'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler.
java.lang.NullPointerException
at akka.http.scaladsl.server.directives.FileAndResourceDirectives.$anonfun$getFromResource$1(FileAndResourceDirectives.scala:106)
...

以下是我尝试过的各种路线的两个示例:

Here are two examples of the various routes I tried:

return route(path("docs", () ->
        getFromResource("resources/index.html")
      ));

return pathPrefix("docs", () ->
    route(
        pathEnd(() -> getFromResource("resources/index.html"))
));

以下行正确打印文件的路径:

The following line correctly prints the path to the file:

try {
       System.out.println(MyDocService.class.getClassLoader().getResource("resources/index.html").toURI());
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我假设传递给getFromResource方法的路径应该相同。由于绝大多数的akka​​-http google搜索都返回Scala示例,因此我敢肯定,编写Java版本会犯一些愚蠢的错误。我发现了一个较旧的StackOverflow帖子,该帖子有关在路径包含空格时加载静态资源的问题,因此我确保服务路径中没有空格。

I assume the path I pass to getFromResource method should be the same. Since the vast majority of akka-http google searches return Scala examples, I'm sure I'm just making some stupid mistake writing a Java version. I found an older StackOverflow post about issues loading static resources when the path contains spaces, so I made sure there are no spaces in the path to my service. Thanks in advance for any pointers.

推荐答案

我能够使用getFromFile而不是getFromResource使它起作用。尚不清楚为什么getFromResource无法正常工作,但我可以解决此问题。

I was able to get this to work using getFromFile instead of getFromResource. It's still not clear why getFromResource does not work, but I'm able to workaround the problem.

这篇关于使用Akka-http Java加载静态html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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