在scala喷雾中,是否有一种将Unmarshaller[T]转换为FromRequestUnmarshaller[T]的方法。我被困在试图使entity指令工作,而不使用implicits。例如:
...
} ~ post {
path("myPath") {
entity(sprayJsonUnmarshaller[MyCaseClass](myCaseClassRootJsonFormat)) { myCaseClass =>
complete { handle(myCaseClass) }
}
} ~ ...编译器错误:
Multiple markers at this line
- type mismatch; found : spray.httpx.unmarshalling.Unmarshaller[MyCaseClass] (which
expands to) spray.httpx.unmarshalling.Deserializer[spray.http.HttpEntity,MyCaseClass]
required: spray.httpx.unmarshalling.FromRequestUnmarshaller[?] (which expands to)
spray.httpx.unmarshalling.Deserializer[spray.http.HttpRequest,?]
- type mismatch; found : spray.httpx.unmarshalling.Unmarshaller[MyCaseClass] (which
expands to) spray.httpx.unmarshalling.Deserializer[spray.http.HttpEntity,MyCaseClass]
required: spray.httpx.unmarshalling.FromRequestUnmarshaller[?] (which expands to)
spray.httpx.unmarshalling.Deserializer[spray.http.HttpRequest,?]发布于 2014-06-26 06:47:36
在这部分,喷雾在很大程度上取决于隐式分辨率。我可能是不正确的,但正如我所知,现在有简单而优雅的方法来做到这一点。就像它的设计一样,您应该执行以下指令:entity(as[MyCaseClass])。然后,如果您看一看as[_]指令,就会发现一个简单的解组器(它接受一个实体并使您的case类)到fromRequestUnmarshaller (从HttpRequest ->案例类),所有隐式扩展程序都可以找到here。因此,当您调用entity(as[MyCaseClass])时,它会扩展到以下内容:
entity {
as[MyCaseClass] {
fromRequestUnmarshaller[MyCaseClass] {
fromMessageUnmarshaller[MyCaseClass] {
sprayJsonUnmarshaller[MyCaseClass](myCaseClassRootJsonFormat)
}
}
}
}如果您想要使它显式,那么您应该用上面的形式写它。这样您就可以丢弃as[MyCaseClass]
另一方面,您可以选择另一个显式的方法-提取实体并将其转换为json:
requestInstance { req =>
val json = req.entity.asString.parseJson
json.convertTo(myCaseClassRootJsonFormat)
}https://stackoverflow.com/questions/24420249
复制相似问题