Akka HTTP 客户端 - 使用 Play 解组 JSON

Akka HTTP client - Unmarshal with Play JSON

我正在使用 Akka HTTP 作为客户端来执行 POST 请求并解析答案。我正在使用 Play 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定义是:

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.HttpResponsePlayJsonSupport 仅支持 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._