我正在尝试重新处理这个或这个,但我不断收到一个我无法修复的错误......
首先,这是我的依赖项:
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上添加了一个示例项目