我是scala和play框架的新手。尝试使用malao reactive和play-framework与scala创建示例应用程序。但我面临一个问题。当我尝试映射我的case类,格式化JSON时,我在json格式化程序中遇到以下编译时错误:
Multiple markers at this line: No unapply function found
之前我也创建了一个简单的示例,但该示例成功运行。这造成了一个问题。以下是我的案例类代码:
case class Video (
var _id: Option[BSONObjectID],
var title: Option[String],
var alias: Option[String],
var categoryIds: Option[List[BSONObjectID]],
var tags: Option[List[String]],
var thumbnailImg: Option[String],
var videoCoverImg: Option[String],
var videoUrl: Option[String],
var isRemote: Option[Boolean],
var userId: Option[BSONObjectID],
var videoEmbedCode: Option[String],
var isPublic: Option[Boolean],
var description: Option[String],
var isPublished: Option[Boolean],
var isRecommended: Option[Boolean],
var access: Option[String],
var uploadedOn: Option[BSONDateTime],
var modifiedOn: Option[BSONDateTime],
var likeCount: Option[Int],
var dislikeCount: Option[Int],
var hitCount: Option[Int],
var metaDesc: Option[String],
var metaKeywords: Option[List[String]],
var author: Option[String]
)
object VideoJsonFormatter {
implicit val videoJsonFormat = Json.format[Video]
}
此implicit val videoJsonFormat = Json.format[Video]
表达式创建编译时错误:
Multiple markers at this line: No unapply function found
我的控制器代码:
def dashboard = Action.async{
logger.info("In dashboard controller method");
var cursor: Cursor[Video] = videosCollection.find(Json.obj()).cursor[Video];
val videosList : Future[List[Video]] = cursor.collect[List](10, true);
videosList.map { videos => Ok(Json.toJson(videos)) }
}
此var cursor: Cursor[Video] = videosCollection.find(Json.obj()).cursor[Video];
表达式生成编译时错误:
No Json deserializer found for type models.Video. Try to implement an implicit Reads or Format for this type.
在我们评论某些属性的情况下,这是成功的。这些属性如下:
var tags: Option[List[String]]
var userId: Option[BSONObjectID]
var metaKeywords: Option[List[String]]
但我需要所有的财产。我怎么能解决这个问题?
答案 0 :(得分:0)
您的案例类包含超过21个字段,这就是为什么错误,您必须为BSONObjectID和BSONDateTime提供隐式格式。
将您的案例类修改为
case class Video (
var _id: Option[BSONObjectID],
var title: Option[String],
var alias: Option[String],
var categoryIds: Option[List[BSONObjectID]],
var tags: Option[List[String]],
var thumbnailImg: Option[String],
var videoCoverImg: Option[String],
var videoUrl: Option[String],
var isRemote: Option[Boolean],
var userId: Option[BSONObjectID],
var videoEmbedCode: Option[String],
var isPublic: Option[Boolean],
var description: Option[String],
var isPublished: Option[Boolean],
var isRecommended: Option[Boolean],
var access: Option[String],
var additional : Additional
)
case class Additional( var uploadedOn: Option[BSONDateTime],
var modifiedOn: Option[BSONDateTime],
var likeCount: Option[Int],
var dislikeCount: Option[Int],
var hitCount: Option[Int],
var metaDesc: Option[String],
var metaKeywords: Option[List[String]],
var author: Option[String])
object AdditionalForm{
implicit val xJsonFormat = Json.format[Additional]
}
object VideoJsonFormatter {
import AdditionalForm._
implicit val videoJsonFormat = Json.format[Video]
}