How to serialize ObjectId to JSON?

北城余情 提交于 2019-12-04 21:50:55

It looks like Jackson has been customized to serialize the string id field in a special way. That is probably a part of the integration with org.bson library.

The problem is that your deserializer is parametrized by the ObjectId type instead of String or plain Object. Try to change it as follows and also remove the @ObjectId annotation from the field declaration. Here is an example:

public class ObjectIdSerializer extends JsonSerializer<Object> {
    @Override
    public void serialize(Object value, JsonGenerator jsonGen,SerializerProvider provider) throws IOException {
        jsonGen.writeString(value.toString());
    }
}

You may also consider adopting the Jackson-Jongo provider class to fix the object id serialization for all the classes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!