2015-07-21 128 views
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 

难道不应该是工作?

+0

确保这些implicits实际上存在那里发生转换 –

+0

通过导入? – buzz2buzz

+0

是的,我不确定Play上的细节,但通过Spray,我通常会忽略一些导入。我只是在AkkaHttp服务上自己修复了相同的问题,并且在文件顶部缺少了这个导入:import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ 我希望这能让您走上正确的轨道 –

回答