Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: Only JsObjects can be stored in Play framework?

Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: Only JsObjects can be stored in Play framework?

我是 Reactivemongo 数据库 (2.6) 的新手,因为我正在尝试 upload/insert json object(这是 key/value 对,我正在发送它从我的 UI 单击提交按钮后存储在数据库中)从我的本地系统存储在 Mongodb 使用 Play Framework/Scala(尝试使用 Play 2.2.3 和 2.3.8)。我试过:

import play.api.libs.json.{JsObject, JsValue, OWrites}
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection._

import scala.concurrent.ExecutionContext.Implicits.global

object Application extends Controller with MongoController {
  def jsinfocollection: JSONCollection = db.collection[JSONCollection]("mycollection")

  implicit val jsvalueWrites = new OWrites[JsValue] {
    println("implicit val jsvalueWrites ...")//getting the message on Play console
    override def writes(o: JsValue): JsObject = o match {
      case o : JsObject => o
      case _ => throw new IllegalArgumentException("Only JsObjects can be stored")
    }
  }

  def createJson = Action.async(parse.json) {
    println("createJson calling...")//getting the message on Play console
    request =>
        jsinfocollection.insert(request.body).map{
    println("jsinfocollection.insert...")//getting the message on Play console
        r => Created(s"JsonInfo is Saved with result $r")
      }
  }
}

我在 Mongodb 中创建了一个 collection 已经喜欢:>db.createCollection("mycollection")

{ "ok" : 1 }

但如果我给出:>db.mycollection.count() - 给出 0,否则如果我给出:db.mycollection.find() - 什么都不给出

请告诉我如何将我的 json 数据插入我需要的 collection("mycollection")。提前致谢。

我在控制台上得到以下信息:

implicit val jsvalueWrites ...
createJson calling...
jsinfocollection.insert...
[error] play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: Only JsObjects can be stored

Internal server error, for (POST) [/createJson] 

当您使用 Action.async(parse.json) 时,您在 request.body

中有一个 JsValue

jsinfocollection只能存储JsObjects(JsObjects只是JsValues的一种,其他类型有JsArray, JsString, ...)

下面的代码应该可以满足您的需求

import play.api.libs.json.{JsObject, JsValue, OWrites}
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection._

import scala.concurrent.ExecutionContext.Implicits.global

object Application extends Controller with MongoController {
  def jsinfocollection: JSONCollection = db.collection[JSONCollection]("mycollection")

  implicit val jsvalueWrites = new OWrites[JsValue] {
    override def writes(o: JsValue): JsObject = o match {
      case o : JsObject => o
      case _ => throw new IllegalArgumentException("Only JsObjects can be stored")
    }
  }

  def createJson = Action.async(parse.json) {
    request =>
      jsinfocollection.insert(request.body).map{
        r => Created(s"JsonInfo is Saved with result $r")
      }
  }
}

也许有更简单的方法来创建 jsvalueWrites 也许更新到最新版本会有所帮助

注意: 字符串是否为有效 JsValue 的验证将由 parse.json 中的框架完成,如果需要,则在 jsvalueWrites 中验证是否为对象完全控制你可以实现 OWrites[String] 并删除 parse.json