2015-01-14 4 views
6

Во время игры с akka-http experimental 1.0-M2 Я пытаюсь создать простой пример Hello 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.scala, RoutingSetup.apply нуждается в нем.

Может быть, команде akka просто нужно добавить еще @implicitNotFound s. Мне удалось найти неточный, но связанный ответ: direct use of Futures in Akka и spray 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() 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^