Jackson adds redundant data to POJO and then cannot read it back

I am using Jackson to send JSON data between the client and server. I am trying to use the full Jackson binding function, and I am applying it to standard POJOs. The problem is that Jackson seems to add redundant data while marshaling on the server, so when I try to cancel it back to POJO on the client side, I get an error.

Here's an excerpt from Jackson's line:

{"_ class": "com.mycoomp.MyObject", "_ identifier": {"time": 1300314145000, "new": false, "machine": 1652794940, "on": - 510750341}, "language": "," a type".....

MyObject contains "language" and "type", but it does not contain "time", "new" and "machine" which are not part of it, but on the client side I get this error:

Unrecognized field "time" (class org.bson.types.ObjectId), not marked as ignorant in [Source: java.io.StringReader@1c56c60 ; row: 1, column: 102] (via the chain of links: com.mycomp.MyObject ["_ id"] → org.bson.types.ObjectId ["time"])

Any ideas ...?

+3
source share
3 answers

, . , ( , ); , @JsonTypeInfo, .

, , , , , ?

+1

/ 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;
+2

, . , mongo-jackson-mapper

, ObjectId.

+1

All Articles