Akka HTTP 客户端 - 使用 Play JSON 进行解编



我正在使用Akka HTTP作为客户端来执行POST请求并解析答案。我正在使用播放 JSON,并收到以下编译器错误:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.javadsl.model.ResponseEntity,B]
[ERROR]       Unmarshal(response.entity).to[B].recoverWith {

这是我为使用Play JSON而不是Spray添加的依赖项:

"de.heikoseeberger" %% "akka-http-play-json"

我的类定义是:

class HttpClient(implicit val system: ActorSystem, val materializer: Materializer) extends PlayJsonSupport {

方法定义为:

private def parseResponse[B](response: HttpResponse)(implicit reads: Reads[B]): Future[B] = {
if (response.status().isSuccess) {
Unmarshal(response.entity).to[B].recoverWith {
....

在进口中,我有:

import play.api.libs.json._
import scala.concurrent.ExecutionContext.Implicits.global
import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._

在我看来,我在范围内具有所需的隐式。Marshal部分具有类似的逻辑(但使用Writes而不是Reads(并且编译良好。我错过了什么?

检查您的其他导入。根据错误消息,您似乎使用的是akka.http.javadsl.model.HttpResponse而不是akka.http.scaladsl.model.HttpResponse;PlayJsonSupport仅支持 Scala DSL:

private def parseResponse[B](response: HttpResponse)(implicit reads: Reads[B]): Future[B] = ???
// ^ this should be akka.http.scaladsl.model.HttpResponse

换句话说,使用

import akka.http.scaladsl.model._

而不是

import akka.http.javadsl.model._

最新更新