Casbah:没有隐式视图可用错误

Casbah: No implicit view available error

在 Play 应用程序中,使用 Salat 和 Casbah,我试图将 DBObject 反序列化为类型 Task 的对象,但在调用 [=14] 时出现此错误=]:

No implicit view available from com.mongodb.casbah.Imports.DBObject => com.mongodb.casbah.Imports.MongoDBObject. Error occurred in an application involving default arguments.

对象已使用 .asDBObject 正确序列化,并按预期写入数据库。

是什么导致了这种行为,可以采取什么措施来解决它?这是涉及的模型:

package models

import db.{MongoFactory, MongoConnection}

import com.novus.salat._
import com.novus.salat.global._
import com.novus.salat.annotations._
import com.mongodb.casbah.Imports._
import com.mongodb.casbah.commons.Imports._
import play.api.Play


case class Task(label: String, _id: ObjectId=new ObjectId)

object Task {

  implicit val ctx = new Context {
    val name = "Custom_Classloader"
  }

  ctx.registerClassLoader(Play.classloader(Play.current))

  val taskCollection = MongoFactory.database("tasks")

  def create(label: String): Task = {
    val task = new Task(label)
    val dbObject = grater[Task].asDBObject(task)
    taskCollection.save(dbObject)
    grater[Task].asObject(dbObject)
  }

  def all(): List[Task] = {
    val results = taskCollection.find()
    val tasks = for (item <- results) yield grater[Task].asObject(item)
    tasks.toList
  }
}

版本

casbah: "2.8.1"
scala: "2.11.6"
salat: "1.9.9"

关于创建自定义上下文的说明:

  • First, define a custom context as implicit val ctx = new Context { /* custom behaviour */ } in a package object
  • Stop importing com.novus.salat.global._
  • Import your own custom context everywhere instead.

来源:https://github.com/novus/salat/wiki/CustomContext