Jackson 正在向 POJO 添加冗余数据,然后无法将其读回
我使用 Jackson 来在客户端和服务器之间发送 JSON 类型的数据。 我正在尝试使用 Jackson 的完整绑定功能,并将其应用到标准 POJO 上。 问题是 Jackson 似乎在服务器上的编组期间添加了冗余数据,因此当我尝试将其解组回客户端的 POJO 时,我收到错误。
以下是杰克逊字符串的摘录:
{“_class”:“com.mycoomp.MyObject”,“_id”:{“time”:1300314145000,“new”:false,“machine”:1652794940,“inc”:-510750341 },"language":"","type".....
MyObject 包含“语言”和“类型”,但不包含不属于其中的“时间”、“新”和“机器”但在客户端,我收到此错误:
无法识别的字段“time”(类org.bson.types.ObjectId),未在[来源:java.io.StringReader@1c56c60;未标记为可忽略;行:1,列:102](通过参考链:com.mycomp.MyObject["_id"]->org.bson.types.ObjectId["time"])
有什么想法......?
I am using Jackson in order to send data in JSON type between a client a server.
I am trying to use Jackson's full binding feature and I am applying it over a standard POJO.
The problem is that Jackson seem to add redundant data during marshaling on the server so when I try to unmarshall it back to the POJO on the client side I'm getting an error.
Here's an excerpt of the Jackson String:
{"_class":"com.mycoomp.MyObject","_id":{"time":1300314145000,"new":false,"machine":1652794940,"inc":-510750341},"language":"","type".....
MyObject contains "language" and "type" but it it doesn't contain “time”, “new” and “machine” that are not part of it but on the client side i'm getting this error:
Unrecognized field "time" (Class org.bson.types.ObjectId), not marked as ignorable at [Source: java.io.StringReader@1c56c60; line: 1, column: 102] (through reference chain: com.mycomp.MyObject["_id"]->org.bson.types.ObjectId["time"])
Any ideas...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案是为 ObjectId 提供自定义序列化器/反序列化器:
并将它们注册为文档。例如,在您的 POJO 中添加:
The solution is to provide a custom serializer/deserializer for ObjectId:
And register them as any of the methods described in the documentation. For example, add in your POJO:
您需要为要序列化的类型提供类型定义。 Jackson 不会添加任何无法从对象中发现的条目(通过 getter、公共字段或显式注释);除非您添加 @JsonTypeInfo 注释来添加类型标识符。
那么也许您正在序列化的对象有更多将被序列化的公共字段?
You need to give type definitions for types you are serializing. Jackson does not add any entries that are not discoverable from objects (via getters, public fields, or explicitly annotated); except in cases where you add @JsonTypeInfo annotation to also add type identifier.
So maybe object you are serializing has more public fields that will be serialized?
我刚刚遇到这个问题,因为我遇到了同样的问题。似乎是 mongo-jackson-mapper 的工作,
我还建议参加基础设施课程例如您域之外的 ObjectId。
I've just come across this as I had the same problem. Seems like a job for the mongo-jackson-mapper
I would also advise taking infrastructural classes such as ObjectId out of your domain.