如何在喷雾框架内提供文件(类型不匹配的 getFromFile)?

how to serve a file within the spray framework (type mismatch getFromFile)?

这可能是一个简单的问题,但我是 Scala 的新手(java 也是)。我正在尝试实现一个喷雾文件服务器。当我返回 Hello 字符串时,一切正常,但是当我尝试使用 getFromFile 提供文件时,我得到:

Error:(16, 24) type mismatch;
found   : spray.routing.Route
(which expands to)  spray.routing.RequestContext => Unit
 required: spray.httpx.marshalling.ToResponseMarshallable
        getFromFile("build.sbt")
                   ^
                        ^  

我该如何解决这个错误?

import akka.actor.ActorSystem
import spray.routing.SimpleRoutingApp

object Main extends SimpleRoutingApp {
  def main(args: Array[String]): Unit = {
    implicit val actorSystem = ActorSystem()
    startServer(interface="localhost", port = 8080) {
        path("File") {
          complete {
            "Hello"
            //getFromFile("build.sbt")
          }
        }
    }
  }
}   

"build.sbt" 不是目录,它是文件,您必须使用文件夹目标。例如getFromDirectory("myfiles") 因为 myfiles 存在于 src/main/resources/myfiles。 如果您只想提供一个文件,例如 index.html,请使用 getFromFile("index.html"),因为此文件存在于 "src/main/resources/index.html"

getFromDirectory docs api

问候。

getFromFile 自动完成请求,所以删除完整的并尝试如下所示。并确保您的工作目录设置为 build.sbt 在当前目录中

object Main extends SimpleRoutingApp {
  def main(args: Array[String]): Unit = {
    implicit val actorSystem = ActorSystem()
    startServer(interface="localhost", port = 8080) {
        path("File") {
            getFromFile("build.sbt")
        }
    }
  }
}