Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

akka http compile error

I am new in akka framework and now try to setup simple webservice with this framework .
write a simple akka-http application :

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
}

receive this error on compile :

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)

How can fix that ?

like image 995
mah454 Avatar asked Dec 15 '16 16:12

mah454


1 Answers

It is just a simple mistake when instantiating your ActorMaterializer:

implicit val materializer = ActorMaterializer

should be replaced by

implicit val materializer = ActorMaterializer()

With a valid materializer in scope, the implicit conversion between the Route and the Flow[HttpRequest, HttpResponse, _] should happen as expected, and the compiler should be happy.

like image 151
Stefano Bonetti Avatar answered Dec 11 '22 07:12

Stefano Bonetti