类型与akka.http.scaladsl.server.Route不匹配

编程入门 行业动态 更新时间:2024-10-22 16:40:08
本文介绍了类型与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?

推荐答案

从文档:

使用 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:

  • 调用 Route.handlerFlow :
  • Call Route.handlerFlow:
  • ...bindAndHandle(Route.handlerFlow(route(store)(logger)), ...)

  • 导入方法在 Route 随播对象中,并执行与上述相同的操作,只不过现在您可以删除对 Route 对象的显式引用:
  • 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)), ...)

  • 导入 akka.http.scaladsl.server.RouteResult ._ :
  • Import akka.http.scaladsl.server.RouteResult._:
  • import akka.http.scaladsl.server.RouteResult._ ...bindAndHandle(route(store)(logger), ...)

    更多推荐

    类型与akka.http.scaladsl.server.Route不匹配

    本文发布于:2023-11-25 13:57:36,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1630033.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:不匹配   类型   akka   scaladsl   http

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!