Akka HTTP 路由 DSL 示例无效

Akka HTTP Routing DSL example is invalid

所以我正在尝试构建一个最小的 HTTP 服务器来接收 POST 请求,但我无法完成它,因为 Akka docs 上的示例不知何故无效,我做不到从任何地方获取 executionContext

这是我的代码:

object Main extends App {

  implicit val system: ActorSystem = ActorSystem("server-system")
  implicit val executionContext = system.executionContext  // can't resolve symbol executionContext

  // test route
  val route =
    path("test") {
      get {
        complete(HttpEntity(ContentTypes.`text/plain(UTF-8)`, "test"))
      }
    }

  val address = "localhost"
  val port = 8080
  val bindingFuture = Http().newServerAt(address, port).bind(route)
  println(s"Server online at $address:$port\nPress RETURN to stop...")

  StdIn.readLine()
  bindingFuture
    .flatMap(_.unbind())                  // no implicits found for parameter
    .onComplete(_ => system.terminate())  // executor: ExecutorContext

}

我使用的是 10.2.1 版,我是用 gradle 导入的,如下所示:

compile group: 'com.typesafe.akka', name: "akka-http_2.12", version: "10.2.1"
compile group: 'com.typesafe.akka', name: "akka-stream_2.12", version: "2.6.8"

您是否有任何其他有效的快速入门示例,或任何其他可以更容易使用的库,以便像我尝试的那样设置一个非常小的服务器?

在 Scala 中定义 ExecutionContext 的方法有很多种。例如,其中 2 个是:

implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.global

或:

implicit val ec: ExecutionContext = system.dispatcher

但在选择你真正想要使用的那个之前,我建议阅读

您还可以阅读Dispatchers by Akka for more customisable options. There is also the post Execution Context and Dispatcher - Best practices, useful configurations and Documentation