我只是想使用 akka 在 scala 项目中实现一个小示例 REST 端点。代码如下
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import scala.io.StdIn
import scala.concurrent.ExecutionContextExecutor
object ViewAPI :
@main def run(): Unit = {
implicit val system = ActorSystem(Behaviors.empty, "my-system")
implicit val executionContext = system.executionContext
val route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
val bindingFuture = Http().newServerAt("localhost", 9001).bind(route)
}
Run Code Online (Sandbox Code Playgroud)
我的 sbt 文件如下所示:
libraryDependencies += ("com.typesafe.akka" %% "akka-http" % "10.2.9").cross(CrossVersion.for3Use2_13)
libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % "2.6.19"
libraryDependencies += "com.typesafe.akka" …Run Code Online (Sandbox Code Playgroud)