当其中一个构造函数参数默认初始化时,如何从Json创建实例?
ADSRegistrationMap中的方法用于从MongoDB保存和检索文档。ObjectId是在初始化期间创建的。我也必须这样做才能从Json加载注册,Json是POST正文的一部分,所以我想我可以添加ADSRegistrationProtocol对象来实现这一点。它因编译错误而失败。有没有办法解决这个问题或者做得更好当其中一个构造函数参数默认初始化时,如何从Json创建实例?,json,mongodb,scala,spray,spray-json,Json,Mongodb,Scala,Spray,Spray Json,ADSRegistrationMap中的方法用于从MongoDB保存和检索文档。ObjectId是在初始化期间创建的。我也必须这样做才能从Json加载注册,Json是POST正文的一部分,所以我想我可以添加ADSRegistrationProtocol对象来实现这一点。它因编译错误而失败。有没有办法解决这个问题或者做得更好 package model import spray.json._ import DefaultJsonProtocol._ import com.mongodb.casb
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请求主体转换为注册对象时立即获取运行时错误。有什么想法吗
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)
为了避免任何问题,我将如下定义Register类(它似乎是一个数据模型
案例类寄存器(系统:字符串,标识:字符串,标识:字符串)
这是因为将id
字段作为字符串而不是BSON ObjectId(我习惯于使用不依赖第三方库的数据模型)对我来说更有意义
因此,正确的JSON协议将使用jsonFormat3
,而不是jsonFormat2
:
对象注册协议扩展了DefaultJsonProtocol{
隐式val registrationFormat=jsonFormat3(注册)
}
这将解决任何类型的JSON序列化问题
最后,您的toBson
和fromBson
转换器将是:
def toBson(r:注册):DBObject={
MongoDBObject(
“系统”->r系统,
“身份”->r.identity,
“\u id”->新对象id(r.id)
)
}
及
def fromBson(o:DBObject):注册={
登记(
系统=o.as[字符串](“系统”),
identity=o.as[String](“identity”),
id=o.as[ObjectId](“_id”).toString
)
}
A这就是使用BSON ObjectId的地方:更接近于依赖MongoDB的逻辑。我认为这与相关,因为默认情况下,
ObjectId
不可序列化。此外,您可能需要将jsonFormat2
更改为jsonFormat3
将ObjectId更新为String,将jsonFormat2更改为jsonFormat3以修复编译错误。现在,将POST请求主体转换为注册实例时出现新的运行时错误。有什么想法吗?是的,我认为您遇到了spray json的兼容性问题,请参阅。降级到1.2.6
,事情就会解决。或者,您也可以复制通知单中描述的SprayJsonSupport
。降级到1.2.6有效。谢谢