Scala 找不到隐式…:akka.http.server.RoutingSetup
在使用akka http 1.0-M2时,我试图创建一个简单的Hello world示例Scala 找不到隐式…:akka.http.server.RoutingSetup,scala,akka,akka-http,Scala,Akka,Akka Http,在使用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
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?"))
}
}
}
}
}
编译失败,无法找到参数设置的隐式值:akka.http.server.RoutingSetup
还有,如果我改变
complete(HttpResponse(entity = "Hello world?"))
与
我得到另一个错误:
类型不匹配;找到:String(“Hello world?”)required:akka.http.marshalling.ToResponseMarshallable
通过研究,我能够理解缺少执行上下文的问题。为了解决这两个问题,我需要包括以下内容:
implicit val executionContext = system.dispatcher
查看akka/http/marshalling/ToResponseMarshallable.scala
我看到ToResponseMarshallable.apply
需要它,它返回一个未来的[HttpResponse]
另外,在akka/http/server/RoutingSetup.scala中,RoutingSetup.apply
需要它
可能akka团队只需要添加更多的@implicitNotFound
s。我在:和上找到了不确切但相关的答案,发现得很好-这个问题在Akka HTTP 1.0-RC2中仍然存在,因此现在的代码必须如下所示(考虑到他们的API更改):
implicit val executionContext = system.dispatcher
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()
}