我使用带有插件的 Play Framework 2.2.1 和 reactivemongo "org.reactivemongo" %% "play2-reactivemongo" % "0.10.2"
我的模型:
package models
import system.db.Mongo
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.Json
import scala.concurrent.Awaitable
import scala.concurrent.duration._
import scala.concurrent.Await
object Template {
import system.db.Mongo.JsonFormats._
def findAll = Await.result(Mongo.templates.find(Json.obj()).cursor[Template].collect[List](), 3 seconds)
}
case class Template(name: String, marks: List[Mark], sections: List[Section])
case class Mark(name: String, description: String, index: Int, indexTree: String, dataType: String, rate: Int)
case class Section(name: String, index: Int, indexTree: String, marks: List[Mark], section: List[Section])
Mongo.templates是一个JSONCollection: val templates: JSONCollection = db.collection[JSONCollection]("Templates")
Json格式:
object JsonFormats {
implicit val markFormat = Json.format[Mark]
implicit val sectionFormat = Json.format[Section]
implicit val templateFormat = Json.format[Template]
}
当我用数据调用Template.findAll
时
{
"name": "Caption",
"sections": [{
"name": "Section name",
"index": 1,
"indexTree": "1",
"marks": [],
"sections": []
}],
"marks": []
}
存在执行异常: [RuntimeException: JsError(List((/section(0)/section,List(ValidationError(error.path.missing,WrappedArray()
)))))]但它适用于空白部分:{"name": "Caption", "sections": [], "marks": []}
此驱动程序是否支持回收结构?
嗯。我已经解决了将归档部分重命名为部分:
case class Template(name: String, marks: List[Mark], sections: List[Section])
case class Mark(name: String, description: String, index: Int, indexTree: String, dataType: String, rate: Int)
case class Section(name: String, index: Int, indexTree: String, marks: List[Mark], parts: List[Section])
收集:
{
"name": "Caption",
"sections": [{
"name": "Section name",
"index": 1,
"indexTree": "1",
"marks": [],
"parts": [{
...
"parts": [...]
}, ...]
}],
"marks": []
}