我是 acala 和 akka 的新手,所以这个问题可能有点傻。
我有一个类:
case class Foo(colUNO: String, colDOS: Long)
我有一个功能:
getById() : Future[Option[Foo]]
我正在尝试在 akka-http 路由中使用它
def main(args: Array[String]) {
implicit val actorSystem = ActorSystem("system")
implicit val actorMaterializer = ActorMaterializer()
val route = pathSingleSlash {
get {
complete {
val fut = getById()
}
}
}
Http().bindAndHandle(route,"localhost",8080)
println("server started at 8080")
但是错误说:
Error:(39, 20) type mismatch; found : scala.concurrent.Future[Option[com.cassandra.phantom.modeling.MiTabla.User]] required: akka.http.scaladsl.marshalling.ToResponseMarshallable getById(id)
我必须做什么才能返回 Foo 的 Json?
谢谢!!
决议:
正在查看:http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.3/scala/http/common/json-support.html 并添加以下代码:
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val userFormat = jsonFormat2(Foo)
}
最佳答案
查看:http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.3/scala/http/common/json-support.html并添加以下代码:
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val userFormat = jsonFormat2(Foo)
}
关于json - 如何在 AKKA-HTTP 中将 Future[Option[Foo]] 类编码为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35758229/