有谁能告诉我为什么我收到以下错误?:
[error] HttpClient.scala:117: type mismatch;
[error] found : akka.stream.scaladsl.Sink[(akka.http.scaladsl.model.StatusCode, String),scala.concurrent.Future[(akka.http.scaladsl.model.StatusCode, String)]]
[error] required: akka.stream.Graph[akka.stream.SinkShape[(akka.http.scaladsl.model.StatusCode, String)],scala.concurrent.Future[akka.http.scaladsl.model.StatusCode]]
[error] source.via(flow).runWith(Sink.head)
[error] ^
以下是代码:
implicit def map2entity: ToEntityMarshaller[Map[String, Any]] = mapMarshaller(MediaTypes.`application/json`)
def mapMarshaller(mediaType: MediaType.WithFixedCharset): ToEntityMarshaller[Map[String, Any]] =
Marshaller.withFixedContentType(mediaType) { m => HttpEntity(mediaType, JSONObject(m).toString()) }
def post(path: String, entity: Map[String, Any]): Future[StatusCode] = {
val uri = Uri(getResourceUri(path))
logger.info(s"httpPost: $uri")
Marshal(entity).to[RequestEntity] flatMap { e =>
val source = Source.single(HttpRequest(
uri = uri.path.toString,
method = HttpMethods.POST,
entity = e))
val flow = getConnection(uri.scheme)(uri.authority.host.address)
.mapAsync(10) { r =>
//(r.status -> Marshal(r.entity).to[String])
Unmarshal(r.entity).to[String].flatMap(s => Future(r.status -> s))
}
source.via(flow).runWith(Sink.head)
}
}
答案 0 :(得分:0)
接收器的实现值(Future[(StatusCode, String)]
)与您在函数中声明的返回类型(Future[StatusCode]
)不同。
如果post
函数只需要返回状态代码,则可以更改此调用
.flatMap(s => Future(r.status -> s))
到此
.map(_ => r.status)