我知道这个问题已被问过千次,但我无法理解为什么这段代码无法正常工作。
case class Partner
(_id: Option[BSONObjectID], name: String, beacon: Option[Beacon])
class PartnerFormatter @Inject() (val beaconDao: BeaconDao){
implicit val partnerReads: Reads[Partner] = (
(__ \ "_id").readNullable[String]and
(__ \ "name").read[String] and
(__ \ "beacon").read[String]
)((_id, name, beaconID) => Partner(_id.map(BSONObjectID(_)), name, Await.result(beaconDao.findById(beaconID), 1 second))))
implicit val partnerWrites: Writes[Partner] = (
(JsPath \ "_id").writeNullable[String].contramap((id: Option[BSONObjectID]) => Some(id.get.stringify)) and
(JsPath \ "name").write[String] and
(JsPath \ "beacon").writeNullable[String].contramap((beacon: Option[Beacon]) => Some(beacon.get._id.get.stringify))
)(unlift(Partner.unapply))
}
我正面临
No Json deserializer found for type models.Partner. Try to implement an implicit Reads or Format for this type
或者
No Json deserializer found for type models.Partner. Try to implement an implicit Writes or Format for this type
不应该有用吗?
答案 0 :(得分:0)
您需要在Partner object 中定义读者和作者。
这是一个例子,https://github.com/luongbalinh/play-mongo/blob/master/app/models/User.scala