适当的方法将objectid映射到弹簧中的dto中串起

发布于 2025-02-04 14:39:39 字数 563 浏览 3 评论 0原文

我正在与Spring Websocket合作。 当我将此DTO发送为响应时,我有Message的DTO

@AllArgsConstructor
@Data
public class MessageDto {

    private ObjectId messageId;
    private ObjectId chatId;
}

objectId tto Maps中的字段如下:

我如何映射<代码> objectid 字段<代码>字符串喜欢62790C02513AD11442EEC6D7

I am working with Spring WebSocket. I have this DTO of the Message

@AllArgsConstructor
@Data
public class MessageDto {

    private ObjectId messageId;
    private ObjectId chatId;
}

When I send this DTO as response, ObjectId fields in DTO maps like this:

enter image description here

How can I map ObjectId field into String like 62790c02513ad11442eec6d7?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

百合的盛世恋 2025-02-11 14:39:39

Spring使用Jackson将对象映射到JSON,因此映射ObjectID字段的正确方法是配置Jackson 连续化器。

创建一个序列化器类:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.bson.types.ObjectId;

import java.io.IOException;

public class ObjectIdSerializer extends JsonSerializer<ObjectId> {

    @Override
    public void serialize(ObjectId id, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
            throws IOException {
        jsonGenerator.writeObject(id == null ? null : id.toString());
    }
}

然后在DTO类中指定序列化器:

@AllArgsConstructor
@Data
public class MessageDto {

    @JsonSerialize(using = ObjectIdSerializer.class)
    private ObjectId messageId;
    @JsonSerialize(using = ObjectIdSerializer.class)
    private ObjectId chatId;
}

总而言之,ObjectId messagedto对象的字段将像json中的字符串一样序列化。

当然,对于对象的 json ,存在一个问题。有jsondeserializer&lt;@jsondeserialize来解决此问题。但是(映射objectId类字段)deserializer是没有用的,因为ObjectId已经具有一个构造函数,它接收string参数,所以杰克逊将使用它。

Spring uses Jackson to map objects to JSON, so the proper way to map ObjectId field in a object is to configure Jackson serializers.

Create a serializer class:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.bson.types.ObjectId;

import java.io.IOException;

public class ObjectIdSerializer extends JsonSerializer<ObjectId> {

    @Override
    public void serialize(ObjectId id, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
            throws IOException {
        jsonGenerator.writeObject(id == null ? null : id.toString());
    }
}

And then specify the serializer in a DTO class:

@AllArgsConstructor
@Data
public class MessageDto {

    @JsonSerialize(using = ObjectIdSerializer.class)
    private ObjectId messageId;
    @JsonSerialize(using = ObjectIdSerializer.class)
    private ObjectId chatId;
}

In conclusion, the ObjectId field of the MessageDto object will be serialized like a string in JSON

Of course, there is a problem to deserialize JSON to an object. There are JsonDeserializer<> and @JsonDeserialize to solve this problem. But there (mapping ObjectId class field) deserializer is useless, because ObjectId already have a constructor, that receives a String parameter, so Jackson will use it.

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