类型与akka.http.scaladsl.server.Route不匹配 [英] type mismatch with 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
orRoute.asyncHandler
aRoute
can be lifted into a handlerFlow
or async handler function to be used with abindAndHandleXXX
call from the Core Server API.
注意: Route $也存在隐式转换c $ c>到
随播广告,它依赖于 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 theRoute
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不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!