[英]Scala activerecord with implicit json format
I have a scala-activerecord: 我有一个scala-activerecord:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person]
Everything works ok. 一切正常。
Suddenly, I want to provide an API and repond with json representation of the entity, so I modified the code: 突然,我想提供一个API并以该实体的json表示作为响应,因此我修改了代码:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person] with DefaultJsonProtocol {
implicit val jsonFormat = jsonFormat1(Request)
}
Now it causes an exception: 现在,它导致一个异常:
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$1.apply(ActiveRecordConfig.scala:29)
at com.github.aselab.activerecord.Config$$anonfun$schema$1.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
EDIT: I put two breakpoints in ActiveRecordConfig.scala
: 编辑:我在
ActiveRecordConfig.scala
放置两个断点:
Breakpoint A here: 断点A:
def schema(companion: ActiveRecordBaseCompanion[_, _]): ActiveRecordTables = {
val clazz = companion.classInfo.clazz
tables.getOrElse(clazz, throw ActiveRecordException.tableNotFound(clazz.toString))
}
Breakpoint B here: 断点B在这里:
def registerSchema(s: ActiveRecordTables) = {
conf = s.config
s.all.foreach(t => _tables.update(t.posoMetaData.clasz, s))
}
With the first code (without json implicit) the execution hits the breakpoint B. 使用第一个代码(不隐含json),执行将到达断点B。
With the second code (including json implicit) the execution hits the breakpoint A first, causing the exception. 使用第二个代码(包括json隐式),执行将首先命中断点A,从而导致异常。
Json support work in the version 0.3.1 of scala-activerecord
, see wiki and this issue . Json在
scala-activerecord
0.3.1版本中支持工作,请参阅Wiki和本期 。 As for now, with the latest version 0.3.0 you can use the first code and the form values deserialization: 到目前为止,在最新版本0.3.0中,您可以使用第一个代码和表单值反序列化:
case class Person(name: String) extends ActiveRecord with Timestamps
object Person extends ActiveRecordCompanion[Person]
And in your eg spray controller: 在您的喷雾控制器中:
import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._
requestContext.complete(Person.find(id).toFormValues)
The method toFormValues
will return Map[String, String]
, which can be by spray-json
implicitly converted to json. toFormValues
方法将返回Map[String, String]
,可以通过spray-json
隐式转换为json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.