Type mismatch with akka.http.scaladsl.server.Route

Hi all

I’ve created a http server with akka http as follows:

import akka.actor.typed.{ActorRef, ActorSystem}
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
import com.sweetsoft.LoggerActor.Log
import akka.actor.typed.scaladsl.adapter._
import akka.http.scaladsl.Http.ServerBinding
import akka.http.scaladsl.model._
import com.sweetsoft._
import akka.http.scaladsl.server.Directives._
import akka.stream.typed.scaladsl.ActorMaterializer

import scala.concurrent.Future

object ProducerActor {

  private val route: Option[ActorRef[ProducerMessage]] => Option[ActorRef[Log]] => Route
  = store => logger =>
    path("producer") {
      post {
        entity(as[ProducerMessage]) { msg =>
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
        }
      }
    }

  def create[A](store: Option[ActorRef[ProducerMessage]], logger: Option[ActorRef[Log]])
               (implicit system: ActorSystem[A])
  : Future[ServerBinding] = {

    implicit val materializer = ActorMaterializer()

    //Please log
    Http()(system.toUntyped).bindAndHandle(route(store)(logger), getServerIp, getServerPort)
  }

}

The compiler complains:

[error] /home/developer/scala/plugger/src/main/scala/com/sweetsoft/producer/ProducerActor.scala:35:56: type mismatch;
[error]  found   : akka.http.scaladsl.server.Route
[error]     (which expands to)  akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]
[error]  required: akka.stream.scaladsl.Flow[akka.http.scaladsl.model.HttpRequest,akka.http.scaladsl.model.HttpResponse,Any]
[error]     Http()(system.toUntyped).bindAndHandle(route(store)(logger), getServerIp, getServerPort)

Do I forget to import any libraries?

According to the documentation, it says:

The conversion from Route to flow can either be invoked explicitly using Route.handlerFlow or, otherwise, the conversion is also provided implicitly by RouteResult.route2HandlerFlow [1].

But it does not work.

Thanks

I had the same problem, found this question and find a solution later. So I will share my findings.

I had this problem when migrating from akka classic to akka typed.

In my case, the problem was missing
implicit val materializer = Materializer(system)
which is required by RouteResult.route2HandlerFlow implicit conversion.