我试图重获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时,它会以内容-Type:text/平原的形式发送到客户机。
有人看到这里的问题了吗?
编辑:我在github https://github.com/ydemartino/spray-test上添加了一个示例项目
发布于 2014-07-11 21:13:29
我创建了一个拉请求来解决您的问题:https://github.com/ydemartino/spray-test/pull/1
必须先声明json协议对象,然后才能隐式使用它。我不太清楚为什么编译器不能解决这个问题,但是将对象声明移到顶部就解决了问题。
对于实际项目,请确保在每个文件中声明包,然后在导入语句中使用这些包。
发布于 2016-10-21 12:26:10
在我的例子中,无法解析的隐格式实例的名称与本地定义相冲突,因此它被隐藏起来。编译器对此保持了亲切的沉默。只是在几个小时的脑震荡后偶然发现的。
https://stackoverflow.com/questions/24704749
复制相似问题