【问题标题】:could not find implicit ...: akka.http.server.RoutingSetup找不到隐式...:akka.http.server.RoutingSetup
【发布时间】:2015-05-07 18:23:52
【问题描述】:

在使用 akka-http 实验性 1.0-M2 时,我正在尝试创建一个简单的 Hello world 示例。

import akka.actor.ActorSystem
import akka.http.Http
import akka.http.model.HttpResponse
import akka.http.server.Route
import akka.stream.FlowMaterializer
import akka.http.server.Directives._

object Server extends App {

  val host = "127.0.0.1"
  val port = "8080"

  implicit val system = ActorSystem("my-testing-system")
  implicit val fm = FlowMaterializer()

  val serverBinding = Http(system).bind(interface = host, port = port)
  serverBinding.connections.foreach { connection ⇒
    println("Accepted new connection from: " + connection.remoteAddress)
    connection handleWith Route.handlerFlow {
      path("") {
        get {
          complete(HttpResponse(entity = "Hello world?"))
        }
      }
    }
  }
}

编译失败,could not find implicit value for parameter setup: akka.http.server.RoutingSetup

另外,如果我改变了

complete(HttpResponse(entity = "Hello world?"))

complete("Hello world?")

我收到另一个错误:type mismatch; found : String("Hello world?") required: akka.http.marshalling.ToResponseMarshallable

【问题讨论】:

    标签: scala akka akka-http


    【解决方案1】:

    通过研究,我能够理解缺少Execution Context 的问题。为了解决这两个问题,我需要包含以下内容:

    implicit val executionContext = system.dispatcher
    

    查看akka/http/marshalling/ToResponseMarshallable.scala 我看到ToResponseMarshallable.apply 需要它,它返回一个Future[HttpResponse]

    另外,akka/http/server/RoutingSetup.scalaRoutingSetup.apply 需要它。

    可能是akka 团队只需要添加更多@implicitNotFounds。我在direct use of Futures in Akkaspray Marshaller for futures not in implicit scope after upgrading to spray 1.2找到了不准确但相关的答案

    【讨论】:

      【解决方案2】:

      很好 - Akka HTTP 1.0-RC2 仍然存在这个问题,因此现在的代码必须如下所示(考虑到他们的 API 更改):

      import akka.actor.ActorSystem
      import akka.http.scaladsl.server._
      import akka.http.scaladsl._
      import akka.stream.ActorFlowMaterializer
      import akka.stream.scaladsl.{Sink, Source}
      import akka.http.scaladsl.model.HttpResponse
      import Directives._
      import scala.concurrent.Future
      
      object BootWithRouting extends App {
      
        val host = "127.0.0.1"
        val port = 8080
      
        implicit val system = ActorSystem("my-testing-system")
        implicit val fm = ActorFlowMaterializer()
        implicit val executionContext = system.dispatcher
      
        val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] =
          Http(system).bind(interface = host, port = port)
      
        serverSource.to(Sink.foreach {
          connection =>
            println("Accepted new connection from: " + connection.remoteAddress)
            connection handleWith Route.handlerFlow {
              path("") {
                get {
                  complete(HttpResponse(entity = "Hello world?"))
                }
              }
            }
        }).run()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-17
        • 2017-08-25
        • 2013-02-04
        • 2017-02-02
        • 2017-03-26
        • 2018-03-13
        • 2017-05-16
        • 1970-01-01
        相关资源
        最近更新 更多