加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在Scala Play应用程序中没有实现OWrites和Reads

发布时间:2020-12-16 21:34:08 所属栏目:安全 来源:网络整理
导读:我有多个模型类都共享相同的属性.由于这个原因,我创造了一个特质,例如: trait Player extends Temp { val gameId: BSONObjectID val personalDetails: abc.PersonalDetails // - comes from shared library}case class FootballPlayer(var _id: Option[BSON
我有多个模型类都共享相同的属性.由于这个原因,我创造了一个特质,例如:

trait Player extends Temp {

  val gameId: BSONObjectID

  val personalDetails: abc.PersonalDetails // <- comes from shared library

}

case class FootballPlayer(var _id: Option[BSONObjectID] = None,gameId: BSONObjectID,personalDetails: abc.PersonalDetails,var created: Option[DateTime] = None,var updated: Option[DateTime] = None
                        ) extends Player

case class VideogamePlayer(var _id: Option[BSONObjectID] = None,var updated: Option[DateTime] = None
                        ) extends Player

所有这些模型,都作为一个伴侣对象play.api.libs.json.Reads和play.api.libs.json.OWrites定义.

例如:

object FootballPlayer {

  import play.api.libs.functional.syntax._
  import play.api.libs.json.Reads._
  import play.api.libs.json._
  import reactivemongo.play.json.BSONFormats.BSONObjectIDFormat

  implicit val footballPlayerReads: Reads[FootballPlayer] = (
    (__  "_id").readNullable[BSONObjectID].map(_.getOrElse(BSONObjectID.generate)).map(Some(_)) and
      (__  "gameId").read[BSONObjectID] and
      (__  "personalDetails").read[abc.PersonalDetails] and
      (__  "created").readNullable[DateTime].map(_.getOrElse(new DateTime())).map(Some(_)) and
      (__  "updated").readNullable[DateTime].map(_.getOrElse(new DateTime())).map(Some(_))
    ) (FootballPlayer.apply _)


  implicit val sharedPersonalDetailsWrites: Writes[abc.PersonalDetails] = abc.PersonalDetails.sharedPersonalDetailsWrites

  implicit val footballPlayerWrites: OWrites[FootballPlayer] = (
    (__  "_id").writeNullable[BSONObjectID] and
      (__  "gameId").write[BSONObjectID] and
      (__  "personalDetails").write[abc.PersonalDetails] and
      (__  "created").writeNullable[DateTime] and
      (__  "updated").writeNullable[DateTime]
    ) (unlift(FootballPlayer.unapply))

}

现在我想将它们存储在不同的集合中,但是我只想要一个DAO,所以我实现了以下几点:

trait PlayerDAO[T <: Player] {

  def findById(_id: BSONObjectID)(implicit reads: Reads[T]): Future[Option[T]]

  def insert(t: T)(implicit writes: OWrites[T]): Future[T]

}

class MongoPlayerDAO[T <: Player] @Inject()(
                                           playerRepository: PlayerRepository[T]
                                         ) extends PlayerDAO[T] {

  def findById(_id: BSONObjectID)(implicit reads: Reads[T]): Future[Option[T]] = playerRepository.findById(_id)

  def insert(t: T)(implicit writes: OWrites[T]): Future[T] = playerRepository.insert(t).map(_ => t)

}

然后,我有以下存储库:

class PlayerService[T <: Player] @Inject()(playerDAO: PlayerDAO[T])(implicit reads: Reads[T],writes: OWrites[T]) {

  def findById(_id: BSONObjectID): Future[Option[T]] = playerDAO.findById(_id)


  def save(t: T): Future[T] = playerDAO.save(t)

}

我的模块如下所示:

class PlayerModule extends AbstractModule with ScalaModule {

  def configure() {
    bind[PlayerDAO[FootballPlayer]].to[MongoPlayerDAO[FootballPlayer]]
    bind[PlayerDAO[VideogamePlayer]].to[MongoPlayerDAO[VideogamePlayer]]
    // ...
    ()
  }
}

在我的Play控制器中,我注入以下内容:

import models.FootballPlayer._
import models.VideogamePlayer._ 

class PlayerController @Inject()(
                                 val messagesApi: MessagesApi,footballPlayerService: PlayerService[FootballPlayer],videogamePlayerService: PlayerService[VideogamePlayer]
                               ) extends Controller with I18nSupport

不幸的是,我得到以下异常:

1) No implementation for
play.api.libs.json.OWrites was bound. 2) No
implementation for play.api.libs.json.OWrites
was bound. 3) No implementation for
play.api.libs.json.Reads was bound. 4) No
implementation for play.api.libs.json.Reads
was bound.

如何解决这个问题?

解决方法

在进行注射时可能无法找到隐含的上下文.尝试在您定义绑定的AbstractModule中包含隐含的含义.

编辑

检查我的解决方案在git. Here

我试图效仿你想要做的任何事情,工作正常.

我不太清楚当前的代码是什么问题,因为我无法访问整个代码,但我认为它与应用程序搜索OWrites和Reads隐含的玩家而不是FootballPlayer或VideoGamePlayer有关

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读