2015-01-14 14 views
6

Trong khi chơi với, akka-http thử nghiệm 1.0-M2 Tôi đang cố gắng tạo một ví dụ đơn giản Hello world.không thể tìm thấy tiềm ẩn ...: 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?")) 
     } 
     } 
    } 
    } 
} 

Compilation không thành công với could not find implicit value for parameter setup: akka.http.server.RoutingSetup

Ngoài ra, nếu tôi thay đổi

complete(HttpResponse(entity = "Hello world?")) 

với

complete("Hello world?") 

tôi nhận được một lỗi: type mismatch; found : String("Hello world?") required: akka.http.marshalling.ToResponseMarshallable

Trả lời

7

Với nghiên cứu tôi đã có thể hiểu được vấn đề thiếu Execution Context. Để giải quyết cả hai vấn đề tôi cần phải bao gồm này:

implicit val executionContext = system.dispatcher 

Nhìn vào akka/http/marshalling/ToResponseMarshallable.scala tôi thấy ToResponseMarshallable.apply đòi hỏi nó trả về một Future[HttpResponse].

Ngoài ra, trong akka/http/server/RoutingSetup.scala, RoutingSetup.apply cần đến.

Có thể là nhóm akka chỉ cần thêm một số chi tiết @implicitNotFound s. Tôi có thể tìm thấy câu trả lời không chính xác nhưng có liên quan tại: direct use of Futures in Akkaspray Marshaller for futures not in implicit scope after upgrading to spray 1.2

2

Tìm thấy - vấn đề này vẫn tồn tại với Akka HTTP 1.0-RC2, vì vậy mã bây giờ phải giống như thế này (được thay đổi 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() 
} 
Các vấn đề liên quan