Rename ObjectId _id to id in jackson deserialization with Jongo and MongoDB

后端 未结 4 1131
夕颜
夕颜 2020-12-21 05:56

I\'ve just started working on a project using the play framework,jongo and MongoDB. The project was initially written in Play 2.1 with pojos with an String id field annotat

4条回答
  •  执念已碎
    2020-12-21 06:20

    ObjectIdSerializer always writes property mapped with @ObjectId to a new instance of ObjectId. This is wrong when you map this property to a String.

    To avoid this behaviour, I've write a NoObjectIdSerializer :

    public class NoObjectIdSerializer extends JsonSerializer {
        @Override
        public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
            jgen.writeString(value);
        }
    }
    

    used like this :

    @ObjectId
    @JsonSerialize(using = NoObjectIdSerializer.class)
    protected final String _id;
    

    There is an open issue.

提交回复
热议问题