如何在默认情况下初始化其中一个构造函数params时从Json创建实例?

时间:2015-01-08 13:43:43

标签: json mongodb scala spray spray-json

ADSRegistrationMap中的方法用于从MongoDB保存和检索文档。 ObjectId是在初始化期间创建的。我必须做同样的事情来加载来自Json的注册,这是POST主体的一部分,所以我想我可以添加ADSRegistrationProtocol对象来做到这一点。它因编译错误而失败。有关如何修复它或更好地做到这一点的想法吗?

package model

import spray.json._
import DefaultJsonProtocol._
import com.mongodb.casbah.Imports._
import org.bson.types.ObjectId
import com.mongodb.DBObject
import com.mongodb.casbah.commons.{MongoDBList, MongoDBObject}

case class Registration(
  system: String, 
  identity: String, 
  id: ObjectId = new ObjectId())

object RegistrationProtocol extends DefaultJsonProtocol {
  implicit val registrationFormat = jsonFormat2(Registration)
}

object RegistrationMap {
  def toBson(registration: Registration): DBObject = {
    MongoDBObject(
      "system"         -> registration.system,
      "identity"       -> registration.identity,
      "_id"            -> registration.id
    )
  }

  def fromBson(o: DBObject): Registration = {
    Registration(
      system = o.as[String]("system"),
      identity = o.as[String]("identity"),
      id = o.as[ObjectId]("_id")
    )
  }
}

编译错误:

[error] /model/Registration.scala:20: type mismatch;
[error]  found   : model.Registration.type
[error]  required: (?, ?) => ?
[error]  Note: implicit value registrationFormat is not applicable here because it comes after the application point and it lacks an explicit result type
[error]   implicit val registrationFormat = jsonFormat2(Registration)
[error]                                                    ^
[error] one error found
[error] (compile:compile) Compilation failed

将ObjectId更新为String,将jsonFormat2更新为jsonFormat3以修复编译错误。

case class Registration(
  system: String, 
  identity: String, 
  id: String = (new ObjectId()).toString())

object RegistrationProtocol extends DefaultJsonProtocol {
  implicit val registrationFormat = jsonFormat3(Registration)
}

将POST请求的主体转换为Registration对象时,立即获得运行时错误。有什么想法吗?

val route: Route = {
  pathPrefix("registrations") {
    pathEnd {
      post {
        entity(as[Registration]) { registration =>

这是build.sbt中的内容

scalaVersion := "2.10.4"

scalacOptions ++= Seq("-feature")

val akkaVersion = "2.3.8"

val sprayVersion = "1.3.1"

resolvers += "spray" at "http://repo.spray.io/"
resolvers += "Sonatype releases" at "https://oss.sonatype.org/content/repositories/releases"

// Main dependencies
libraryDependencies ++= Seq(
    "com.typesafe.akka" %% "akka-actor" % akkaVersion,
    "com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
    "com.typesafe.akka" %% "akka-camel" % akkaVersion,
    "io.spray" % "spray-can" % sprayVersion,
    "io.spray" % "spray-routing" % sprayVersion,
    "io.spray" % "spray-client" % sprayVersion,
    "io.spray" %% "spray-json" % sprayVersion,
    "com.typesafe" % "config" % "1.2.1",
    "org.apache.activemq" % "activemq-camel" % "5.8.0",
    "ch.qos.logback" % "logback-classic" % "1.1.2",
    "org.mongodb" %% "casbah" % "2.7.4"
)

错误:

12:33:03.477 [admcore-microservice-system-akka.actor.default-dispatcher-3] DEBUG s.can.server.HttpServerConnection - Dispatching POST request to http://localhost:8878/api/v1/adsregistrations to handler Actor[akka://admcore-microservice-system/system/IO-TCP/selectors/$a/1#-1156351415]
Uncaught error from thread [admcore-microservice-system-akka.actor.default-dispatcher-3] shutting down JVM since 'akka.jvm-exit-on-fatal-error' is enabled for ActorSystem[admcore-microservice-system]
java.lang.NoSuchMethodError: spray.json.JsonParser$.apply(Ljava/lang/String;)Lspray/json/JsValue;
    at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller$1.applyOrElse(SprayJsonSupport.scala:36)
    at spray.httpx.SprayJsonSupport$$anonfun$sprayJsonUnmarshaller$1.applyOrElse(SprayJsonSupport.scala:34)

1 个答案:

答案 0 :(得分:0)

为了避免任何问题,我将如下定义Register类(它似乎是一个数据模型)

case class Register(system: String, identity: String, id: String)

那是因为我将id字段作为字符串而不是BSON ObjectId(我习惯于不依赖于第三方库的数据模型)更有意义)。

因此,正确的SprayJson协议将使用jsonFormat3而不是jsonFormat2

object RegistrationProtocol extends DefaultJsonProtocol {
  implicit val registrationFormat = jsonFormat3(Registration)
}

这将解决任何类型的JSON序列化问题。

最后,您的toBsonfromBson转换器将是:

def toBson(r: Registration): DBObject = {
    MongoDBObject(
      "system"    -> r.system,
      "identity"  -> r.identity,
      "_id"       -> new ObjectId(r.id)
    )
  }

def fromBson(o: DBObject): Registration = {
    Registration(
      system = o.as[String]("system"),
      identity = o.as[String]("identity"),
      id = o.as[ObjectId]("_id").toString
    )
  }

正在使用BSON ObjectId的地方:更接近MongoDB相关逻辑。