2015-01-14 63 views
6

當玩,AKK-HTTP實驗1.0-M2我試圖創建一個簡單的Hello世界的例子。找不到隱含的...:akka.http.server.RoutingSetup

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

回答

7

通過研究,我能夠理解缺少Execution Context的問題。爲了解決這兩個我需要包括這個問題:

implicit val executionContext = system.dispatcher 

展望akka/http/marshalling/ToResponseMarshallable.scala我看到ToResponseMarshallable.apply需要它返回一個Future[HttpResponse]

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

可能是阿卡隊只需要添加一些@implicitNotFound s。我能找到不是確切但相關的答案在: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() 
} 
相關問題