On Saturday, November 21, 2015 at 1:13:01 AM UTC-7, Dennis O wrote:
>
> Hi,
>
> Beginner question on Akka-http 2.
> I have a GET route that will expose a value that I need to get from a 
> model.
> Inside that model, I have Slick returning a Future.
>
> What is the "Akka-native" way to do this, i.e.  Slick giving Future in the 
> model  -> return to router/controller  -> expose as Json?
>

If you don't need to do any additional processing, then the idiomatic way 
to do this would be to return the Future to your routes, then just complete.

First, you need to decide on a Json library. I really like PlayJson.

Add your JSON library to build.sbt:


libraryDependencies ++= Seq(
  "de.heikoseeberger" %% "akka-http-json4s" % "1.2.1",
  "com.typesafe.play" %% "play-json" % "2.4.3"
)





case class User(id: Int, email: String)

object Users {
  def get(id: Int): Future[User] = {
    // ...
  }
}

object JsonFormats {
  // Create a read/write serializer for the User case class
  implicit val userFormat = Json.format[User]
}

class HttpService {
  import JsonFormats._
  import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._
  val route =
    path("user" / IntValue) { userId =>
      val user: Future[User] = Users.get(userId)
      complete(user)
    }
}

Just like that. The import 
de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._ tells akka-http to use 
Play-Json to look for implicit PlayJson formats in scope, and use them to 
output Json. Importing JsonFormats._ makes the User Json format visible to 
de.heikoseeberger.akkahttpplayjson.PlayJsonSupport.
 

>
> And also, do we actually need a Future in this case? Won't a 
> standard/blocking return be appropriate as front-end is waiting for 
> endpoint return anyways (and btw., waiting with a Promise of its own = in 
> Angular)?
>

Using a Future in Scala prevents Scala from blocking a thread. It's 
orthogonal to whether or not the client is blocking or not.
 

>
> Thanks!
> Dennis
>
>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to