George C
George C

Reputation: 1223

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

Upvotes: 11

Views: 6330

Answers (2)

George C
George C

Reputation: 1223

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

Upvotes: 2

Pim Verkerk
Pim Verkerk

Reputation: 1066

This wil deal with your Future problem. And will work if you hava something which converts Option[com.cassandra.phantom.modeling.MiTabla.User]] to a ToResponseMarshallable

val route = pathSingleSlash {
    get { ctx =>
      ctx.complete(getById())
    }
}

Upvotes: 0

Related Questions