2

ADSRegistrationMap 中的方法用于从 MongoDB 保存和检索文档。ObjectId 在初始化期间创建。我必须这样做才能从作为 POST 主体一部分的 Json 加载注册,所以我想我可以添加 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)
4

1 回答 1

0

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

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

那是因为将id字段作为 String 而不是 BSON ObjectId 对我来说更有意义(我习惯于不依赖于 3rd 方库的数据模型)。

因此,正确的 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 相关逻辑。

于 2015-03-17T23:03:17.037 回答