【问题标题】:How to embed Play 2.6 using Akka HTTP Server?如何使用 Akka HTTP Server 嵌入 Play 2.6?
【发布时间】:2017-12-31 04:17:01
【问题描述】:

Play 2.5 默认使用 Netty,并允许轻松嵌入。 https://www.playframework.com/documentation/2.5.x/ScalaEmbeddingPlay

现在如何使用 Akka HTTP Server(现在是默认服务器后端)来完成这项工作?

页面https://www.playframework.com/documentation/2.6.x/ScalaEmbeddingPlay 丢失。

【问题讨论】:

    标签: scala playframework


    【解决方案1】:

    文档分为两页:

    1. 对于 Akka HTTP 服务器:https://www.playframework.com/documentation/2.6.1/ScalaEmbeddingPlayAkkaHttp
    2. 对于 Netty 服务器:https://www.playframework.com/documentation/2.6.1/ScalaEmbeddingPlayNetty

    您应该使用AkkaHttpServerComponents 来避免已弃用的 API:

    import play.api.{ BuiltInComponents, NoHttpFiltersComponents }
    import play.api.mvc._
    import play.api.routing.Router
    import play.api.routing.sird._
    import play.core.server.AkkaHttpServerComponents
    
    val server = new AkkaHttpServerComponents with BuiltInComponents with NoHttpFiltersComponents {
    
      // To avoid using the deprecated Action builder while
      // keeping the `Action` idiom.
      private val Action = defaultActionBuilder
    
      override def router: Router = Router.from {
        case GET(p"") => Action {
          Results.Ok("Hello, World")
        }
      }
    
    
    }.server
    

    【讨论】:

    • sbt 依赖是"com.typesafe.play" %% "play-akka-http-server" % playVersion
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    相关资源
    最近更新 更多