-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when loading static content using AKKA-HTTP Java #2247
Comments
Trying to load a resource that isn't on the classpath returns |
I've inspected the jar file and can find swagger-ui files in the classpath
|
A closer look at the code in FileAndResourceDirectives.scala, seems that null is returned for directories, which might explain the NPE.
|
Doc on |
Probably, it's because the I guess, before we added the Btw: path("swagger", () -> route (
getFromResource("swagger-ui/index.html"),
getFromResourceDirectory("swagger-ui")
) I wonder why that works at all. Shouldn't |
The exception is thrown at line 286 of FileAndResourceDirectives.scala Also, I added pathEndOrSingleSlash and got the same NPE result as before.
|
It's not because |
OK then, so below comment from @jrudolph could be the fix for jar files
|
Actually, that statement was a bit sloppy. In fact, it depends how your classloaders are set up. It seems you are using some spring stuff, so it will depend on how your application is deployed exactly (we cannot really help with that). Regarding your routes, this is still not correct: path("swagger", () -> route (
pathEndOrSingleSlash(() -> getFromResource("swagger-ui/index.html")),
pathEndOrSingleSlash(() -> getFromResourceDirectory("swagger-ui"))
) It should be more like this: pathPrefix("swagger", () -> route ( // everything that's inside `/swagger`
pathEndOrSingleSlash(() -> getFromResource("swagger-ui/index.html")), // `/swagger/` or `/swagger`
getFromResourceDirectory("swagger-ui") // everything else in `/swagger/...`
) I marked this issue as a bug, because we need to improve the error message and not throw an NPE when a resource cannot be found. |
The classloader / spring issue probably isn't a bug so it would make sense to continue those discussions on discuss.akka.io. |
@jrudolph Most java applications use Spring, so I think it'll make sense to add support for Spring class loaders. For reference, below is the full content of the files in my jar file
|
It doesn't only depend on the contents of jar files but how the custom classloaders in your environment deal with those jar files. That's something we cannot debug here (but if you find out, it would make sense to add to the documentation). |
@jrudolph I got it working by adding BOOT-INF to the path as follows
|
@chineduekwunife-kcom great! |
Ran into the exact same problem. When trying to pull a resource using getFromResource when in a spring boot classloader is in the mix... null reference is throw when it cannot find the resourcePath. I don't think it's been said... Spring Boot class loader is doing this to the resource url ... I know this is an implementation detail brought to you by Spring Boot & Company (see spring-projects/spring-boot#7096), but can't akka-http team provide a simple directive for handling boot loader implementation rather than guys like us having to write hacks or custom directives for a spring boot class loader? The above direct path to BOOT-INF is kinda hacky and will only work when referring to resource directory from jar. |
Maybe, even if it's completely unrelated to akka-http, let's document for the |
I guess it already works correctly for simple class loaders. It seems that the spring class loader delivers an invalid URL for |
I'm trying out AKKA-Http in Java for the first time(akka-http_2.12 v 10.1.5, akka-stream_2.12 v 2.5.17), but can't seem to load static content for swagger UI. I've added the routes below and able to reach the json and yaml swagger endpoints via http://localhost:8080/api-docs/swagger.json and http://localhost:8080/api-docs/swagger.yaml However, the swagger UI endpoint i.e http://localhost:8080/swagger returns with 'There was an internal server error.' even though I have swagger-ui directory under src/main/resources folder. I've inspected the logs and don't quite understand what is happening..seems like a bug in getFromResource and getFromResourceDirectory functions. Similar issue was reported earlier
The text was updated successfully, but these errors were encountered: