2016-12-15 52 views
5

我是akka框架中的新成员,现在尝试使用此框架设置简单的webservice。
写一个简单的阿卡-HTTP应用: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 
} 

收到编译此错误:

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) 

如何解决呢?

回答

13

实例化时,它仅仅是一个简单的错误你ActorMaterializer

implicit val materializer = ActorMaterializer 

implicit val materializer = ActorMaterializer() 

被替换为范围的有效materializer的RouteFlow[HttpRequest, HttpResponse, _]之间的隐式转换应按预期发生,编译器应该很高兴。

+1

这是一个无赖。我们还向IntelliJ提出了一个问题,以警告以下类型的问题:https://youtrack.jetbrains.com/issue/SCL-12026 – jrudolph