Jackson 正在向 POJO 添加冗余数据,然后无法将其读回

发布于 2024-10-23 15:58:26 字数 577 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娜些时光,永不杰束 2024-10-30 15:58:26

解决方案是为 ObjectId 提供自定义序列化器/反序列化器:

public class ObjectIdMapping {
    public static class ObjectIdSerializer extends JsonSerializer<ObjectId>{
        @Override
        public void serialize(ObjectId  id, JsonGenerator json,
                SerializerProvider provider) throws IOException,
                JsonProcessingException {
            json.writeString(id.toString());

        }
    }
    public static class ObjectIdDeerializer extends JsonDeserializer<ObjectId>{
        @Override
        public ObjectId deserialize(JsonParser jp, DeserializationContext context)
                throws IOException, JsonProcessingException {
            if (!ObjectId.isValid(jp.getText())) throw context.mappingException("invalid ObjectId " + jp.getText());
            return new ObjectId(jp.getText());
        }
    }
}   

并将它们注册为文档。例如,在您的 POJO 中添加:

@JsonSerialize(using = ObjectIdMapping.ObjectIdSerializer.class)
@JsonDeserialize(using = ObjectIdMapping.ObjectIdDeerializer.class)
public ObjectId od;

The solution is to provide a custom serializer/deserializer for ObjectId:

public class ObjectIdMapping {
    public static class ObjectIdSerializer extends JsonSerializer<ObjectId>{
        @Override
        public void serialize(ObjectId  id, JsonGenerator json,
                SerializerProvider provider) throws IOException,
                JsonProcessingException {
            json.writeString(id.toString());

        }
    }
    public static class ObjectIdDeerializer extends JsonDeserializer<ObjectId>{
        @Override
        public ObjectId deserialize(JsonParser jp, DeserializationContext context)
                throws IOException, JsonProcessingException {
            if (!ObjectId.isValid(jp.getText())) throw context.mappingException("invalid ObjectId " + jp.getText());
            return new ObjectId(jp.getText());
        }
    }
}   

And register them as any of the methods described in the documentation. For example, add in your POJO:

@JsonSerialize(using = ObjectIdMapping.ObjectIdSerializer.class)
@JsonDeserialize(using = ObjectIdMapping.ObjectIdDeerializer.class)
public ObjectId od;
浅黛梨妆こ 2024-10-30 15:58:26

您需要为要序列化的类型提供类型定义。 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?

云淡月浅 2024-10-30 15:58:26

我刚刚遇到这个问题,因为我遇到了同样的问题。似乎是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文