1
我知道這個問題已被問過千次,但我不知道爲什麼這個代碼無法正常工作。JSON格式與播放2.4 /斯卡拉
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
難道不應該是工作?
確保這些implicits實際上存在那裏發生轉換 –
通過導入? – buzz2buzz
是的,我不確定Play上的細節,但通過Spray,我通常會忽略一些導入。我只是在AkkaHttp服務上自己修復了相同的問題,並且在文件頂部缺少了這個導入:import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ 我希望這能讓您走上正確的軌道 –