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

时间:2016-01-28 13:39:17

标签: scala spray

这可能是一个简单的问题,但我是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")
          }
        }
    }
  }
}   

2 个答案:

答案 0 :(得分:1)

" build.sbt"不是目录,它是文件,您必须使用文件夹目标。例如自getFromDirectory("myfiles")存在myfiles以来src/main/resources/myfiles index.html。 如果您只想提供getFromFile("index.html")这样的文件,请使用fork in Test := true javaOptions in Test := Seq("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=9003 ") ,因为此文件存在于" src / main / resources / index.html"

getFromDirectory docs api

问候。

答案 1 :(得分:1)

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")
        }
    }
  }
}