2016-11-20 4 views
0

Ich möchte einen Abrufclient in akka-http schreiben, der alle Antwortkörper in ein Play JsObject konvertiert. Was ich bisher habe, ist Code, der this library Code verwendet, der die Dinge einfach machen sollte (ich denke?). Jedoch wenn ich versuche, den Code auszuführen unten ich folgende Fehlermeldung erhalten:Verwenden von PlayJson im akka-http-Client

Error:(26, 56) type mismatch; 
found : akka.http.scaladsl.unmarshalling.FromEntityUnmarshaller[play.api.libs.json.JsObject] 
    (which expands to) akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.scaladsl.model.HttpEntity,play.api.libs.json.JsObject] 
required: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.scaladsl.model.HttpResponse,play.api.libs.json.JsObject] 
    Unmarshaller.byteStringUnmarshaller.mapWithCharset { (data, charset) => 

Was muss ich ändern, was zu bekommen wie erwartet?

import java.util.UUID 

import akka.actor.ActorSystem 
import akka.http.scaladsl.Http 
import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse} 
import akka.stream.ActorMaterializer 
import akka.stream.scaladsl.Source 
import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller} 
import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._ 
import play.api.libs.json.{JsObject, Json} 

import scala.concurrent.duration._ 
import scala.util.{Success, Try} 


object Main extends App { 

    implicit val system = ActorSystem("TestSys") 
    implicit val ec = system.dispatcher 
    implicit val materializer = ActorMaterializer() 

    implicit val um:Unmarshaller[HttpResponse, JsObject] = { 
    Unmarshaller.byteStringUnmarshaller.mapWithCharset { (data, charset) => 
     Json.parse(data.toArray).as[JsObject] 
    } 
    } 

    val request = HttpRequest(uri="https://www.google.com/finance/info?q=INDEXDB%3ADAX") -> UUID.randomUUID() 
    val pool = Http().superPool[UUID]() 
    val googleFinanceFlow = 
    Source.tick(0 milliseconds,10000 milliseconds,request) 
     .via(pool) 
    .runForeach { 
     case (Success(response),_) => 
     val json = Unmarshal(response).to[JsObject] 
     println(json.onSuccess{case r => println(r.toString())}) 
     case _ => Json.obj() 
    } 
} 

Antwort

0

Löschen Sie einfach die explizite implizite (wow, das klingt gut, nicht wahr?) Definition des Unmarshaller[HttpResponse, JsObject]. Das wird nicht benötigt, da ein passender Unmarshaller von PlayJsonSupport bereitgestellt wird.

Verwandte Themen