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

时间:2018-10-06 18:45:47

标签: java akka swagger akka-stream akka-http

我正在尝试Java中的AKKA-Http(akka-http_2.12 v 10.1.5,akka-stream_2.12 v 2.5.17),但似乎无法为静态加载静态内容用户界面。我已经在下面添加了路由,并能够通过http://localhost:8080/api-docs/swagger.jsonhttp://localhost:8080/api-docs/swagger.yaml到达json和yaml swagger端点。但是,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]

1 个答案:

答案 0 :(得分:0)

回答我的问题,通过将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/...`
                )
        ));