gpt4 book ai didi

java - 使用 AKKA-HTTP Java 加载静态内容时出错

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:08 27 4
gpt4 key购买 nike

我第一次在 Java 中尝试 AKKA-Http(akka-http_2.12 v 10.1.5、akka-stream_2.12 v 2.5.17),但似乎无法为 swagger UI 加载静态内容。我添加了下面的路由,并且能够通过 http://localhost:8080/api-docs/swagger.json 到达 json 和 yaml swagger 端点和http://localhost:8080/api-docs/swagger.yaml但是,swagger UI 端点即 http://localhost:8080/swagger返回“存在内部服务器错误。”,即使我在 src/main/resources 文件夹下有 swagger-ui 目录。我检查了日志,不太明白发生了什么......似乎是 getFromResource 和 getFromResourceDirectory 函数中的错误。 Similar issue was reported earlier

Route createRoute() {
return route(
path(PathMatchers.segment(generator.apiDocsPath()).slash("swagger.yaml"),
() -> get(() -> complete(generator.generateSwaggerYaml()))),
path(PathMatchers.segment(generator.apiDocsPath()).slash("swagger.json"),
() -> get(() -> complete(generator.generateSwaggerJson()))),
path("swagger", () -> route (
getFromResource("swagger-ui/index.html"),
getFromResourceDirectory("swagger-ui")
)
)
);
}

akka-spring-docker-system | 2018-10-06 08:27:11.292 ERROR 9 --- [lt-dispatcher-8] akka.actor.ActorSystemImpl : 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.
akka-spring-docker-system |
akka-spring-docker-system | java.lang.NullPointerException: null
akka-spring-docker-system | at akka.http.scaladsl.server.directives.FileAndResourceDirectives$ResourceFile$.apply(FileAndResourceDirectives.scala:286) ~[akka-http_2.12-10.1.5.jar!/:10.1.5]
akka-spring-docker-system | at akka.http.scaladsl.server.directives.FileAndResourceDirectives.$anonfun$getFromResource$2(FileAndResourceDirectives.scala:106) ~[akka-http_2.12-10.1.5.jar!/:10.1.5]
akka-spring-docker-system | at scala.Option.flatMap(Option.scala:171) ~[scala-library-2.12.4.jar!/:na]
akka-spring-docker-system | at akka.http.scaladsl.server.directives.FileAndResourceDirectives.$anonfun$getFromResource$1(FileAndResourceDirectives.scala:106) ~[akka-http_2.12-10.1.5.jar!/:10.1.5]

最佳答案

回答我的问题,通过将 BOOT-INF 添加到路径中来实现此功能,如下所示

 return route(
path(PathMatchers.segment(generator.apiDocsPath()).slash("swagger.yaml"),
() -> get(() -> complete(generator.generateSwaggerYaml()))),
path(PathMatchers.segment(generator.apiDocsPath()).slash("swagger.json"),
() -> get(() -> complete(generator.generateSwaggerJson()))),
pathPrefix("swagger", () -> route ( // everything that's inside `/swagger`
pathEndOrSingleSlash(() -> getFromResource("BOOT-INF/classes/swagger-ui/index.html")), // `/swagger/` or `/swagger`
getFromResourceDirectory("BOOT-INF/classes/swagger-ui") // everything else in `/swagger/...`
)
));

关于java - 使用 AKKA-HTTP Java 加载静态内容时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682275/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com