Scala Akka HTTP客户端-使用Play JSON解组
我使用Akka HTTP作为客户端来执行POST请求并解析答案。我正在使用Play JSON,出现以下编译器错误:Scala Akka HTTP客户端-使用Play JSON解组,scala,unmarshalling,akka-http,play-json,Scala,Unmarshalling,Akka Http,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].recoverWi
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 {
....
方法定义为:
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._
在我看来,我在其范围内具有所需的含义。
封送处理
部分具有类似的逻辑(但具有写入
而不是读取
),并且编译良好。我遗漏了什么?检查您的其他导入。根据错误消息,您使用的似乎是akka.http.javadsl.model.HttpResponse
,而不是akka.http.scaladsl.model.HttpResponse
;仅支持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._
检查您的其他导入。根据错误消息,您使用的似乎是
akka.http.javadsl.model.HttpResponse
,而不是akka.http.scaladsl.model.HttpResponse
;仅支持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自动添加了导入,甚至没有注意到这一点。另一双眼睛总是有帮助的,非常感谢!哦哇!愚蠢的错误。我用IntelliJ自动添加了导入,甚至没有注意到这一点。另一双眼睛总是有帮助的,非常感谢!