2

如何解决从 Spray(或 Akka-Http)提供 ​​html 时缺少静态内容的问题?我的服务的基本网址是/api(即使在这种情况下它应该是无关紧要的)。

这是我的路线

get {
  pathPrefix("swagger") {
    pathEndOrSingleSlash {
      getFromResource("swagger-ui/index.html")
    } ~
      getFromResourceDirectory("swagger-ui")
  }
}

当我打开加载的 html 时,可以找到 css 和 js 文件

/api/swagger/

但是当我打开

/api/swagger(没有尾部斜杠)

加载的 html 尝试从中获取内容

/api/css/reset.css代替/api/swagger/css/reset.css

我应该如何重写我的路线以涵盖这两种情况?

4

2 回答 2

2

我最终添加了重定向。如果有人知道更优雅的解决方案,请发布。

pathPrefix("swagger") {
  CachingDirectives.cachingProhibited {
    pathEnd {
      redirect("/api/swagger/", StatusCodes.TemporaryRedirect)
    } ~
      pathSingleSlash {
        getFromResource("swagger-ui/index.html")
      } ~
      getFromResourceDirectory("swagger-ui")
  }
}
于 2015-01-20T13:32:41.330 回答
2

请改用该redirectToTrailingSlashIfMissing指令。

参考:http ://doc.akka.io/docs/akka/2.4/scala/http/routing-dsl/directives/path-directives/redirectToTrailingSlashIfMissing.html

于 2016-09-09T12:28:16.170 回答