java静态html文件,使用Akka-http Java加载静态html文件

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

}

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值