【发布时间】:2014-09-02 11:50:28
【问题描述】:
我正在尝试重新处理 this 或 this,但我不断收到无法修复的错误...
首先,这是我的依赖项:
compile 'io.spray:spray-can_2.11:1.3.1'
compile 'io.spray:spray-routing_2.11:1.3.1',
compile 'io.spray:spray-json_2.11:1.2.6'
现在我想做的是:
class WHttpService extends Actor with HttpService with ActorLogging {
implicit def actorRefFactory = context
def receive = runRoute(route)
lazy val route = logRequest(showReq _) {
// Way too much imports but I tried all I could find
import spray.json._
import DefaultJsonProtocol._
import MasterJsonProtocol._
import spray.httpx.SprayJsonSupport._
path("server" / Segment / DoubleNumber / DoubleNumber) { (login, first, second) =>
get {
complete {
Answer(1, "test")
}
}
}
}
private def showReq(req : HttpRequest) = LogEntry(req.uri, InfoLevel)
}
与:
case object MasterJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport {
import spray.json._
case class Answer(code: Int, content: String)
implicit val anwserFormat: JsonFormat[Answer] = jsonFormat2(Answer)
}
现在我收到此错误:
Error:(42, 19) type mismatch;
found : MasterJsonProtocol.Answer
required: spray.httpx.marshalling.ToResponseMarshallable
Answer(1, "test")
^
我尝试了很多东西,但无法让它发挥作用。 我试过了
Answer(1, "test").toJson
Answer(1, "test").toJson.asJsObject
最后我做的是
complete {
Answer(1, "test").toJson.compactPrint
}
这可行,但是当我需要 application/json 时,它会作为 Content-Type: text/plain 发送给客户端。
有人知道这里有什么问题吗?
编辑:我在github上添加了一个示例项目https://github.com/ydemartino/spray-test
【问题讨论】:
-
+1。这是一篇很棒的第一篇文章,欢迎关注 SO :D
-
我应该在顶级评论中问这个问题。您使用的是哪个版本的 Scala?看起来 Spray 还没有发布 2.11 github.com/spray/spray/issues/790
-
我看到了这个问题,但是根据官方文档:spray.io/project-info/current-versions "spray 1.3.1 是针对 Scala 2.10.3 和 Akka 2.3.0 以及 Scala 2.11.1 和 Akka 2.3 构建的.2。”因为我可以使用 'io.spray:spray-can_2.11:1.3.1' 获取文件,所以我认为他们同时修复了它。我将尝试使用 scala 2.10 来查看我的代码是否可以编译。
-
我在 github 上添加了一个示例项目,可以重现问题。本项目使用 scala 2.10:github.com/ydemartino/spray-test
标签: scala spray spray-json