11

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)
}
George C
  • 1,168
  • 13
  • 30

2 Answers2

2

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)
}
George C
  • 1,168
  • 13
  • 30
0

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())
    }
}
Pim Verkerk
  • 1,066
  • 7
  • 12
  • thanks @Pim Verkerk !! How can I convert Option[com.cassandra.phantom.modeling.MiTabla.User]] to a ToResponseMarshallable ?? I think I have to use the akka-http-spray-json but I don' t know how – George C Mar 03 '16 at 19:22
  • 1
    I found the answer!! -> Resolution is in the question – George C Mar 03 '16 at 21:12