2016-12-15 12 views
5

Tôi mới trong khuôn khổ akka và bây giờ cố gắng thiết lập webservice đơn giản với khung công tác này.
viết một đơn giản AKKA-http ứng dụng:lỗi biên dịch akka http

import akka.actor.ActorSystem 
import akka.http.scaladsl.Http 
import akka.http.scaladsl.server.Directives._ 
import akka.stream.ActorMaterializer 

import scala.io.StdIn 

object MainRunner extends App { 

    implicit val system = ActorSystem("mySystem") 
    implicit val materializer = ActorMaterializer 
    implicit val ec = system.dispatcher 

    val route = 
    path("hello") { 
     get { 
     complete("Congratulation , this is your response") 
     } 
    } 

    val bindingFuture = Http().bindAndHandle(route, "localhost", 8080) 

    println(s"Server online at http://localhost:8080/\nPress RETURN to stop...") 
    StdIn.readLine() // let it run until user presses return 
    bindingFuture 
    .flatMap(_.unbind()) // trigger unbinding from the port 
    .onComplete(_ => system.terminate()) // and shutdown when done 
} 

nhận lỗi này trên biên dịch:

Error:(34, 44) type mismatch; 
found : akka.http.scaladsl.server.Route 
    (which expands to) akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult] 
required: akka.stream.scaladsl.Flow[akka.http.scaladsl.model.HttpRequest,akka.http.scaladsl.model.HttpResponse,Any] 
    val bindingFuture = Http().bindAndHandle(route, "localhost", 8080) 

Làm thế nào có thể khắc phục điều đó?

Trả lời

13

Nó chỉ là một sai lầm đơn giản khi instantiating bạn ActorMaterializer:

implicit val materializer = ActorMaterializer 

nên được thay thế bởi

implicit val materializer = ActorMaterializer() 

Với materializer hợp lệ trong phạm vi, việc chuyển đổi ngầm giữa RouteFlow[HttpRequest, HttpResponse, _] nên xảy ra như mong đợi, và trình biên dịch nên được hạnh phúc.

+1

Đó là một con số thấp. Chúng tôi cũng đã gửi một vấn đề với IntelliJ để cảnh báo chống lại các loại sự cố sau: https://youtrack.jetbrains.com/issue/SCL-12026 – jrudolph