问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/52226298/loading-static-html-files-using-akka-http-java