Akka-Http:如何在请求中使用 Actor?

Akka-Http: How to use an Actor in a request?

基于 Akka-HTTP 的这个简单代码:

val route =
    pathPrefix("myapp") {
      path("search") {
        get {
          //ref ! DoSomething("foo")
          complete(HttpEntity(ContentTypes.`application/json`, /* content here from an actor */ ))
        }
      }
    }

如何 return 来自 Actor (sender ! content) 的值?

使用 ask 模式并映射它的 return 未来。

import akka.pattern.ask    // enable `?`
import context.dispatcher  // Future's need an execution context, we use the Actor#context's one 

(ref ? DoSomething("foo")).mapTo[ReturningType].map { result =>
  complete(HttpEntity(ContentTypes.`application/json`, result ))
}