本文介绍了类型与akka.http.scaladsl.server.Route不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
调用 Route.handlerFlow :
Call Route.handlerFlow:
导入方法在 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:
导入 akka.http.scaladsl.server.RouteResult ._ :
Import akka.http.scaladsl.server.RouteResult._:
我已经使用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:
...bindAndHandle(Route.handlerFlow(route(store)(logger)), ...)
import akka.http.scaladsl.server.Route import akka.http.scaladsl.server.Route._ ...bindAndHandle(handlerFlow(route(store)(logger)), ...)
import akka.http.scaladsl.server.RouteResult._ ...bindAndHandle(route(store)(logger), ...)
更多推荐
类型与akka.http.scaladsl.server.Route不匹配
发布评论