【问题标题】:ObjectId is not serialized to JSONObjectId 未序列化为 JSON
【发布时间】:2014-11-25 14:02:44
【问题描述】:

我正在使用 scalatra 并将我的 servlet 配置为始终返回 JSON(如相应指南中所述)。使用 MongoDB 和 Salat 引导我将 MongoDBObject 读回我的案例类 - 这似乎工作得很好。

我的案例类:

import org.bson.types.ObjectId
import com.novus.salat.annotations.raw.Key

case class Player(_id: ObjectId, firstName: String, ...)

打印案例类对象输出如下:

Player(547489ee93f4272e548ded63,Peter,...)

如您所见,objectid 是 org.bson.types.ObjectId。 自动序列化为 JSON 将其发送到浏览器:

{"_id":{},"firstName":"Peter",...}

我的 ObjectID 在哪里?我做错了什么?

【问题讨论】:

    标签: json mongodb scalatra objectid json4s


    【解决方案1】:

    我在网上找到了以下内容: https://gist.github.com/dozed/5631680

    经过一个小测试后,似乎我所要做的就是更改我的 servlet 中的代码

    protected implicit val jsonFormats: Formats = DefaultFormats
    

    protected implicit val jsonFormats: Formats = DefaultFormats + new ObjectIdSerializer
    

    并添加

    import org.json4s.mongo.ObjectIdSerializer
    

    也许这会帮助另一个 Scalatra-NOOB... ;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2013-05-11
      • 1970-01-01
      • 2013-01-17
      • 2013-01-01
      • 2017-06-25
      • 1970-01-01
      相关资源
      最近更新 更多