【问题标题】:Akka HTTP client - Unmarshal with Play JSONAkka HTTP 客户端 - 使用 Play JSON 解组
【发布时间】:2018-05-04 17:49:49
【问题描述】:

我使用 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 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)并且编译良好。我错过了什么?

【问题讨论】:

    标签: scala unmarshalling akka-http play-json


    【解决方案1】:

    检查您的其他进口商品。根据错误消息,您似乎使用的是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._
    

    【讨论】:

    • 哦..哇。愚蠢的错误。我使用 IntelliJ 自动添加了导入,甚至没有注意到这一点。另一双眼睛总是有帮助,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多