找不到隐含的...:akka.http.server.RoutingSetup
could not find implicit ...: akka.http.server.RoutingSetup
在玩 akka-http experimental 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
通过研究,我能够理解缺少 Execution Context
的问题。为了解决这两个问题,我需要包括这个:
implicit val executionContext = system.dispatcher
查看 akka/http/marshalling/ToResponseMarshallable.scala
我发现 ToResponseMarshallable.apply
需要 returns Future[HttpResponse]
。
此外,在akka/http/server/RoutingSetup.scala
中,RoutingSetup.apply
需要它。
可能是 akka 团队需要再添加一些 @implicitNotFound
。我能够在以下位置找到不准确但相关的答案:direct use of Futures in Akka and spray Marshaller for futures not in implicit scope after upgrading to spray 1.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()
}
在玩 akka-http experimental 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
通过研究,我能够理解缺少 Execution Context
的问题。为了解决这两个问题,我需要包括这个:
implicit val executionContext = system.dispatcher
查看 akka/http/marshalling/ToResponseMarshallable.scala
我发现 ToResponseMarshallable.apply
需要 returns Future[HttpResponse]
。
此外,在akka/http/server/RoutingSetup.scala
中,RoutingSetup.apply
需要它。
可能是 akka 团队需要再添加一些 @implicitNotFound
。我能够在以下位置找到不准确但相关的答案:direct use of Futures in Akka and spray Marshaller for futures not in implicit scope after upgrading to spray 1.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()
}