Nit*_*... 6 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 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?"))
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
编译失败 could not find implicit value for parameter setup: akka.http.server.RoutingSetup
另外,如果我改变了
complete(HttpResponse(entity = "Hello world?"))
Run Code Online (Sandbox Code Playgroud)
同
complete("Hello world?")
Run Code Online (Sandbox Code Playgroud)
我收到另一个错误: type mismatch; found : String("Hello world?") required: akka.http.marshalling.ToResponseMarshallable
通过研究,我能够理解缺乏的问题Execution Context.要解决这个问题,我需要包含这个:
implicit val executionContext = system.dispatcher
Run Code Online (Sandbox Code Playgroud)
调查akka/http/marshalling/ToResponseMarshallable.scala我看到ToResponseMarshallable.apply需要它返回一个Future[HttpResponse].
此外,在akka/http/server/RoutingSetup.scala,RoutingSetup.apply需要它.
可能是akka团队只需要添加更多的@implicitNotFounds.我能够找到不完全但相关的答案:在Akka中直接使用Futures并在升级到spray 1.2之后将Marshaller喷射到不在隐含范围内的期货