如何在Play Framework 2.x中实现Future对象的隐式Json Writes

时间:2016-05-16 10:03:18

标签: json scala playframework

我是Play框架的新手,我想定期向亚马逊询问一些产品,以便将它们插入到kafka主题中,当我尝试编译代码时会发生错误。

这是KafkaProducer的代码:

file example.model.AmazonProducerExample

// UPDATED方法和用户的建议,谢谢你们!

package example.utils

import jodd.lagarto.dom.{NodeSelector, LagartoDOMBuilder}
import example.model.AmazonProduct
import scala.collection.JavaConversions._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

import play.api.libs.json._
import example.utils._
import example.producer._

object AmazonPageParser {

private val topicName = "amazonRatingsTopic"

private val producer = Producer[String](topicName)


def parse(productId: String): Future[AmazonProduct] = {

val url = s"http://www.amazon.com/dp/$productId"
HttpClient.fetchUrl(url) map {
  httpResponse =>
    if (httpResponse.getStatusCode == 200) {
      val body = httpResponse.getResponseBody
      val domBuilder = new LagartoDOMBuilder()
      val doc = domBuilder.parse(body)

      val responseUrl = httpResponse.getUri.toString
      val nodeSelector = new NodeSelector(doc)
      val title = nodeSelector.select("span#productTitle").head.getTextContent
      val img = nodeSelector.select("div#main-image-container img").head.getAttribute("src")
      val description = nodeSelector.select("div#feature-bullets").headOption.map(_.getHtml).mkString

      val amazonProduct = AmazonProduct(productId, title, responseUrl, img, description)

      println("amazonProduct is " + amazonProduct.toString)
      amazonProduct
    } else {
      println("An error happened!")
      throw new RuntimeException(s"Invalid url $url")
    }
}//map
}//parse method

def main(args: Array[String]): Unit = {

//Scala Puzzlers...
AmazonPageParser.parse("0981531679").onSuccess { case amazonProduct =>

  implicit val amazonFormat = Json.format[AmazonProduct]
  producer.send(Json.toJson(amazonProduct).toString)
  println("amazon product sent to kafka cluster..." + amazonProduct.toString)
}
}
}

file example.model.Models

package example.model

import play.api.libs.json.Json
import reactivemongo.bson.Macros

case class AmazonProduct(itemId: String, title: String, url: String, img: String, description: String)
case class AmazonRating(userId: String, productId: String, rating: Double)

case class AmazonProductAndRating(product: AmazonProduct, rating: AmazonRating)

// For MongoDB
object AmazonRating {
implicit val amazonRatingHandler = Macros.handler[AmazonRating]
implicit val amazonRatingFormat = Json.format[AmazonRating]
}

file example.utils.AmazonPageParser

编译器将此错误返回给我:

[error] /Users/aironman/my-recommendation-spark-engine/src/main/scala/example/producer/AmazonProducerExample.scala:25: No Json serializer found for type scala.concurrent.Future[example.model.AmazonProduct]. Try to implement an implicit Writes or Format for this type.
[error]     producer.send(Json.toJson(amazonProduct).toString)
[error]                              ^

我引用了这个post with most votes,但它对我不起作用。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

Writes[T]生成Json。您无法直接从Future生成它而不会阻止。

但是你可以在这个未来添加“回调”,如下所示:

amazonPageParser.parse(productId).onSuccess { case amazonProduct =>
   producer.send(Json.toJson(amazonProduct).toString)
}

或使用其他Future方法,例如mapforeach