2016-08-23 3 views
0

私は戻って、このように、リモートのサードパーティのAPIからJSONフィードを取得:Akka:返されたJsonフィードを処理するには?

val myjson: Future[HttpResponse] = http.singleRequest(HttpRequest(uri = encodedUri)) 

myjson onComplete { 
    case Success(response) => sender ! WrappedResponse(response.entity.toJson) 
    case Failure... 
} 

ケースクラスのWrappedResponse(レスポンス:JsValue)

HttpResponse.entityは私のJSONフィードが含まれています。それは可能なマーシャルとunmarshallこのJsonのフィードまたはその一部だけですか?

Error:(38, 78) Cannot find JsonWriter or JsonFormat type class for akka.http.scaladsl.model.ResponseEntity 
     case Success(response) => sender ! WrappedResponse(response.entity.toJson) 

どのように私は「マーシャル」JSON自体:

問題の一つは、私はそれを送り返すときということですが、私は何かを得るケースクラスでJSONを包ん?

UPDATE

私は最終的に、このように最初のデータをアンマーシャリングするために得る:

val responseData = sendHttpRequest(encodedUrl) 
     val bodyAsString = responseData.flatMap { response => Unmarshal(response.entity).to[String] } 

     bodyAsString onComplete { 
     case Success(body) => sender ! WrappedResponse(body) 
     case Failure(response) => response 
     } 

と私のマーシャラーで:

trait MyJsonMarshaller extends SprayJsonSupport with DefaultJsonProtocol { 

    implicit val titleDeedResponseFormat = jsonFormat1(WrappedResponse.apply) 

} 

が、 "再適用" マーシャリング動作していません

答えて

0

確かに!これは、テストされていないと私自身のプロジェクトにコードから適応ですが、あなたが何か行うことができます。この場合

import akka.http.scaladsl.unmarshalling.Unmarshal 

val myResponse: Future[HttpResponse] = http.singleRequest(HttpRequest(uri = encodedUri)) 

val jsonFut = myResponse.flatMap { 
    case HttpResponse(StatusCodes.OK, _, entity, _) => 
    Unmarshal(entity).to[JsObject] 
    case other => 
    logger.debug(s"Failed response: $other") 
    throw new Exception(other.toString()) 
}.recover { 
    // ... error handling 
} 

を、あなたは、アドホックな方法でJsObject結果に対処するでしょう。しかし、ドメインモデルクラスのインスタンスをJsonFormatと定義すると、ドメインオブジェクトに直接アンマーシャリングできます。

+0

「マーシャリングを再適用する」とはどういう意味ですか?コンパイラエラーが発生しましたか? – acjay

+0

いいえ、私はそれを解決しました。もう一度申し込む必要はありません。 – Randomize

関連する問題