具有隐式 json 格式的 Scala activerecord
Scala activerecord with implicit json format
我有一个 scala-activerecord:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person]
一切正常。
突然想提供一个API并用实体的json表示回复,所以修改了代码:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person] with DefaultJsonProtocol {
implicit val jsonFormat = jsonFormat1(Request)
}
现在引发异常:
com.github.aselab.activerecord.SchemaSettingException: Cannot find table definition of class Person
at com.github.aselab.activerecord.ActiveRecordException$.tableNotFound(ActiveRecordException.scala:48)
at com.github.aselab.activerecord.Config$$anonfun$schema.apply(ActiveRecordConfig.scala:29)
at com.github.aselab.activerecord.Config$$anonfun$schema.apply(ActiveRecordConfig.scala:29)
at scala.collection.MapLike$class.getOrElse(MapLike.scala:128)
at scala.collection.AbstractMap.getOrElse(Map.scala:59)
at com.github.aselab.activerecord.Config$.schema(ActiveRecordConfig.scala:29)
at com.github.aselab.activerecord.ActiveRecordBaseCompanion$class.schema(ActiveRecord.scala:116)
at Person$.schema$lzycompute(Request.scala:12)
at Person$.schema(Request.scala:12)
at com.github.aselab.activerecord.ActiveRecordBaseCompanion$class.table(ActiveRecord.scala:123)
at Person$.table$lzycompute(Request.scala:12)
at Person$.table(Request.scala:12)
at com.github.aselab.activerecord.ActiveRecordBaseCompanion$class.all(ActiveRecord.scala:133)
at Person$.all(Request.scala:12)
at com.github.aselab.activerecord.inner.CompanionIterable$class.companionToIterable(Implicits.scala:15)
at Person$.companionToIterable(Request.scala:12)
at Person$.<init>(Request.scala:13)
at Person$.<clinit>(Request.scala)
... 34 more
编辑:
我在 ActiveRecordConfig.scala
:
中放置了两个断点
断点A在这里:
def schema(companion: ActiveRecordBaseCompanion[_, _]): ActiveRecordTables = {
val clazz = companion.classInfo.clazz
tables.getOrElse(clazz, throw ActiveRecordException.tableNotFound(clazz.toString))
}
此处为断点 B:
def registerSchema(s: ActiveRecordTables) = {
conf = s.config
s.all.foreach(t => _tables.update(t.posoMetaData.clasz, s))
}
使用第一个代码(没有 json 隐式)执行到达断点 B。
第二个代码(包括 json 隐式)执行首先到达断点 A,导致异常。
Json 支持 scala-activerecord
版本 0.3.1 中的工作,请参阅 wiki and this issue。至于现在,最新版本0.3.0你可以使用第一个代码和表单值反序列化:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person]
在你的例子中喷雾控制器:
import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._
requestContext.complete(Person.find(id).toFormValues)
方法toFormValues
会returnMap[String, String]
,可以通过spray-json
隐式转换为json.
我有一个 scala-activerecord:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person]
一切正常。
突然想提供一个API并用实体的json表示回复,所以修改了代码:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person] with DefaultJsonProtocol {
implicit val jsonFormat = jsonFormat1(Request)
}
现在引发异常:
com.github.aselab.activerecord.SchemaSettingException: Cannot find table definition of class Person
at com.github.aselab.activerecord.ActiveRecordException$.tableNotFound(ActiveRecordException.scala:48)
at com.github.aselab.activerecord.Config$$anonfun$schema.apply(ActiveRecordConfig.scala:29)
at com.github.aselab.activerecord.Config$$anonfun$schema.apply(ActiveRecordConfig.scala:29)
at scala.collection.MapLike$class.getOrElse(MapLike.scala:128)
at scala.collection.AbstractMap.getOrElse(Map.scala:59)
at com.github.aselab.activerecord.Config$.schema(ActiveRecordConfig.scala:29)
at com.github.aselab.activerecord.ActiveRecordBaseCompanion$class.schema(ActiveRecord.scala:116)
at Person$.schema$lzycompute(Request.scala:12)
at Person$.schema(Request.scala:12)
at com.github.aselab.activerecord.ActiveRecordBaseCompanion$class.table(ActiveRecord.scala:123)
at Person$.table$lzycompute(Request.scala:12)
at Person$.table(Request.scala:12)
at com.github.aselab.activerecord.ActiveRecordBaseCompanion$class.all(ActiveRecord.scala:133)
at Person$.all(Request.scala:12)
at com.github.aselab.activerecord.inner.CompanionIterable$class.companionToIterable(Implicits.scala:15)
at Person$.companionToIterable(Request.scala:12)
at Person$.<init>(Request.scala:13)
at Person$.<clinit>(Request.scala)
... 34 more
编辑:
我在 ActiveRecordConfig.scala
:
断点A在这里:
def schema(companion: ActiveRecordBaseCompanion[_, _]): ActiveRecordTables = {
val clazz = companion.classInfo.clazz
tables.getOrElse(clazz, throw ActiveRecordException.tableNotFound(clazz.toString))
}
此处为断点 B:
def registerSchema(s: ActiveRecordTables) = {
conf = s.config
s.all.foreach(t => _tables.update(t.posoMetaData.clasz, s))
}
使用第一个代码(没有 json 隐式)执行到达断点 B。
第二个代码(包括 json 隐式)执行首先到达断点 A,导致异常。
Json 支持 scala-activerecord
版本 0.3.1 中的工作,请参阅 wiki and this issue。至于现在,最新版本0.3.0你可以使用第一个代码和表单值反序列化:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person]
在你的例子中喷雾控制器:
import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._
requestContext.complete(Person.find(id).toFormValues)
方法toFormValues
会returnMap[String, String]
,可以通过spray-json
隐式转换为json.