json - 如何在 AKKA-HTTP 中将 Future[Option[Foo]] 类编码为 JSON

标签 json scala http akka akka-http

我是 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/

相关文章:

c# - 如何模拟 JsonReader 单元测试自定义 JsonConverter

Java 服务器端发送支持恢复的文件?

php - 在 PHP 中发送 HTTP GET 请求(不返回目标 URL 的内容,只是执行它)

scala - 将数据帧列中的空值替换为其他数据帧中的值到 id

design-patterns - Scala 程序员 - "should there be one obvious way to do it"还是 "more than one way to do it"?

scala - Scala 的 Java EE 替代品?

java - HttpUrlConnection 卡住了我的 AsyncTask

c# - 如何将对象数组转换为 json

xml - JAX RS - JSON 和 XML 循环/循环引用错误

java - 有点迷路…… jackson 反序列化