Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Marshall a Future[Option[Foo]] class to JSON in AKKA-HTTP

Im new to acala and akka, so the question may be a little silly.

I have a class:

case class Foo(colUNO: String, colDOS: Long)

I have a funtion:

getById() : Future[Option[Foo]]

And I am trying to use it in the akka-http route

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")

}

But the error says:

Error:(39, 20) type mismatch; found : scala.concurrent.Future[Option[com.cassandra.phantom.modeling.MiTabla.User]] required: akka.http.scaladsl.marshalling.ToResponseMarshallable getById(id)

What I have to do to return a Json of Foo?

Thanks!!


RESOLUTION:

looking at: http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.3/scala/http/common/json-support.html and adding the following code:

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._

trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
  implicit val userFormat = jsonFormat2(Foo)
}
like image 254
George C Avatar asked Mar 02 '16 21:03

George C


1 Answers

Looking at: http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.3/scala/http/common/json-support.html and adding the following code:

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._

trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
  implicit val userFormat = jsonFormat2(Foo)
}
like image 144
George C Avatar answered Nov 17 '22 22:11

George C