3
votes
  • Akka Http version: "com.typesafe.akka" %% "akka-http" % "10.0.11"
  • Stream version: "com.typesafe.akka" %% "akka-stream" % "2.5.7"
  • Play Json version: "com.typesafe.play" %% "play-json" % "2.6.7"

I have the following method in the crudService:

def getAll: Future[Seq[A]]

I want to return this in a route to provide the outcome as Json to the world. I currently have this:

val crudService = new CrudService[Todo]()



val route =
    pathPrefix("todo" / "_all") {
      get {
        complete {
          crudService.getAll
        }
      }
    }



val bindingFuture = Http().bindAndHandle(route, hostname, port)

I have also tried this (first complete the Future):

val route =
    pathPrefix("todo" / "_all") {
      get {
        onSuccess(crudService.getAll) { x =>
          complete x
        }
      }
    }

It keeps saying: Not applicable to ToResponseMarshallable. I can't find documentation that leads to a correct solution and I don't understand exactly the problem here. Can somebody help out?

1

1 Answers

0
votes

You need add play json support which is not built-in to provide (un)marshalling support.

Try to import https://github.com/hseeberger/akka-http-json library and mix your class with PlayJsonSupport. You can view an example App here.

def route(implicit mat: Materializer) = {
  import Directives._
  import PlayJsonSupport._

  pathSingleSlash {
    post {
      entity(as[Foo]) { foo =>
        complete {
          foo
        }
      }
    }
  }
}

Http().bindAndHandle(route, "127.0.0.1", 8000)