类型与akka.http.scaladsl.server.Route不匹配 [英] type mismatch with akka.http.scaladsl.server.Route

查看:215
本文介绍了类型与akka.http.scaladsl.server.Route不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用akka http创建了一个http服务器,如下所示:

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)
  }

}

编译器抱怨:

[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?

推荐答案

从< a href = https://doc.akka.io/docs/akka-http/10.1.9/routing-dsl/routes.html rel = nofollow noreferrer>文档:


使用 Route.handlerFlow Route.asyncHandler Route 可以提升为处理程序 Flow 或异步处理程序函数,以与 bindAndHandleXXX 。

Using Route.handlerFlow or Route.asyncHandler a Route can be lifted into a handler Flow or async handler function to be used with a bindAndHandleXXX call from the Core Server API.

注意: Route RouteResu中定义的Flow [HttpRequest,HttpResponse,单位] lt 随播广告,它依赖于 Route.handlerFlow

因此,您至少有三个选择:

Therefore, you have at least three options:


  1. 调用 Route.handlerFlow

  1. Call Route.handlerFlow:



...bindAndHandle(Route.handlerFlow(route(store)(logger)), ...)




  1. 导入方法在 Route 随播对象中,并执行与上述相同的操作,只不过现在您可以删除对 Route 对象的显式引用:

  1. Import the methods in the Route companion object and do the same as above, except now you can drop the explicit reference to the Route object:



import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.Route._

...bindAndHandle(handlerFlow(route(store)(logger)), ...)




  1. 导入 akka.http.scaladsl.server.RouteResult ._

  1. Import akka.http.scaladsl.server.RouteResult._:



import akka.http.scaladsl.server.RouteResult._

...bindAndHandle(route(store)(logger), ...)

这篇关于类型与akka.http.scaladsl.server.Route不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆