未找到类型 Seq[models.Comment] 的 Json 格式化程序尝试为此类型实现隐式格式

No Json formatter found for type Seq[models.Comment] Try to implement an implicit Format for this type

你好 scala 和 playFramework 大师,

我不明白为什么隐式 Json.format[Comment] 的用法在我的代码中不起作用。根据文档描述,它应该像 Format[Comment] 一样工作,但看起来不像。

这是我的两个案例的代码 类 和他们的同伴

case class ServiceTask(id: Option[String],
                       name: String,
                       description: String,
                       requiredInfo: String,
                       status: String,
                       approved: Boolean,
                       comments: Option[Seq[Comment]])
object ServiceTask {

  implicit val serviceTaskFormat: Format[ServiceTask] = (
    (JsPath \ "id").formatNullable[String] and
    (JsPath \ "name").format[String] and
    (JsPath \ "description").format[String] and
    (JsPath \ "requiredInfo").format[String] and
    (JsPath \ "status").format[String] and
    (JsPath \ "approved").format[Boolean] and
    (JsPath \ "comments").formatNullable[Seq[Comment]]
  )(ServiceTask.apply _, unlift(ServiceTask.unapply))

}

  case class Comment(id: Option[String],
                     authorID: BSONObjectID,
                     updatedAt: Option[DateTime] = None,
                     body: String)

  object Comment {
    implicit val commentFormat = Json.format[Comment]
  }

您似乎没有在 object serviceTaskFormat 中导入 commentFormat。试试这个:

object ServiceTask {
  import Comment._

  implicit val serviceTaskFormat: Format[ServiceTask] = (
    (JsPath \ "id").formatNullable[String] and
    (JsPath \ "name").format[String] and
    (JsPath \ "description").format[String] and
    (JsPath \ "requiredInfo").format[String] and
    (JsPath \ "status").format[String] and
    (JsPath \ "approved").format[Boolean] and
    (JsPath \ "comments").formatNullable[Seq[Comment]]
  )(ServiceTask.apply _, unlift(ServiceTask.unapply))
}