找不到类型为reactivemongo.play.json.JSONSerializationPack.type的JsObject的Json序列化程序

时间:2016-05-17 15:13:17

标签: json scala play-reactivemongo

我正在使用play framework 2.5.3 with reactive mongoDB。

import javax.inject._

import model._
import play.api.Logger
import play.api.libs.json._
import play.api.mvc._
import play.modules.reactivemongo._
import reactivemongo.api.ReadPreference
import reactivemongo.play.json._
import reactivemongo.play.json.collection._

import scala.concurrent.{ExecutionContext, Future}


class InsertController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionContext) extends Controller with MongoController with ReactiveMongoComponents {

def dataFuture: Future[JSONCollection] = database.map(_.collection[JSONCollection]("data"))

def createFromJson = Action.async(parse.json) { request =>
Json.fromJson[jsonWrapper](request.body) match {
  case JsSuccess(data, _) =>
    for {
      data <- dataFuture
      lastError <- data.insert(data)
    } yield {
      Logger.debug(s"Successfully inserted with LastError: $lastError")
      Ok("Inserted into db")

    }
  case JsError(errors) =>
    Future.successful(BadRequest("Something went wrong"))
}
}

这是我的控制器,在编译时我得到以下异常:

[info] Compiling 6 Scala sources and 1 Java source to /home/***/target/scala-2.11/classes...
[error] /home/***/app/controllers/InsertController.scala:38: No Json serializer as JsObject found for type reactivemongo.play.json.JSONSerializationPack.type. Try to implement an implicit OWrites or OFormat for this type.
[error]           lastError <- data.insert(data.pack)
[error]                                   ^
[error] one error found
[error] (compile:compile) Compilation failed
[info] Compiling 6 Scala sources and 1 Java source to /home/***/target/scala-2.11/classes...
[error] /home/***/app/controllers/InsertController.scala:38: No Json serializer as JsObject found for type reactivemongo.play.json.JSONSerializationPack.type. Try to implement an implicit OWrites or OFormat for this type.
[error]           lastError <- data.insert(data.pack)
[error]                                   ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] application - 

! @705di1397 - Internal server error, for (GET) [/] ->

play.sbt.PlayExceptions$CompilationException: Compilation error[No Json serializer as JsObject found for type reactivemongo.play.json.JSONSerializationPack.type. Try to implement an implicit OWrites or OFormat for this type.]
    at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
    at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27)
    at scala.Option.map(Option.scala:145)
    at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49)
    at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44)
    at scala.Option.map(Option.scala:145)
    at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44)
    at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40)
    at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)
    at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17)

它建议为JSONCollection写一个Owrites或OFormat,它是reactivemongo.play.json._包的一部分,应该已经根据我的理解定义了这些。

这是我的jsonWrapper类:

case class jsonWrapper(tables : tables, userId : String)


object jsonWrapper{
  implicit val jsonRead: Reads[jsonWrapper] = (
    (JsPath \ "tables").read[tables] and
      (JsPath \ "userID").read[String]
    )(jsonWrapper.apply _)

  implicit val jsonWrites: Writes[jsonWrapper] = (
    (JsPath \ "tables").write[tables] and
      (JsPath \ "userID").write[String]
    )(json => (json.tables, json.userId))

  implicit val jsonWrapperFormat : Format[jsonWrapper]  = Json.format[jsonWrapper]

}

表类还具有隐含定义的格式,读取和写入。

我正式使用此示例开始: https://github.com/jonasanso/play-reactive-mongo-db#master,但是当我尝试根据我的需要调整它时(即使用我的jsonWrapper类)我得到了这个错误并且不明白为什么它不起作用。

非常感谢,

Peter M。

1 个答案:

答案 0 :(得分:0)

我发现了我的错误。

def createFromJson = Action.async(parse.json) { request =>
Json.fromJson[jsonWrapper](request.body) match {
  case JsSuccess(data, _) =>
    for {
      data <- dataFuture
      lastError <- data.insert(data)
    } yield {
      Logger.debug(s"Successfully inserted with LastError: $lastError")
      Ok("Inserted into db")

    }
  case JsError(errors) =>
    Future.successful(BadRequest("Something went wrong"))
}

我的案例条目启动了一个名为“data”的对象,我通过覆盖我的dataFuture对象来跟进。因此,造成错误。我只需更改变量名称......

我有点傻了。