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

I just started working on a project using the play, jongo and MongoDB platform. The project was originally written in Play 2.1 with pojos with a String ID field annotated with both: @Id and @ObjectId. This will be saved in Mongo as an ObjectId, and when deserialized it will output id like: "id": "53fcb9ede4b0b18314098d10", for example,

Since switching to Jongo 1.1 and Play 2.3.3, the id attribute is always called "_id" when deserializing, I want the attribute to keep the field name, but I can not use @JsonProperty ("custom_name") as the Jongo @Id annotation does @ JsonProperty ("_ id") backstage.

import org.jongo.marshall.jackson.oid.Id;
import org.jongo.marshall.jackson.oid.ObjectId;

public class PretendPojo {

    @Id
    @ObjectId
    private String id;

    private String name;

    public PretendPojo() {
    }

    public PretendPojo(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

POJO, MongoDB, , RoboMongo

{
    "_id" : ObjectId("53fc984de4b0c34f1905b8ee"),
    "name" : "Owen"
}

, , json, :

{"name":"Owen","_id":{"time":1409072858000,"date":1409072858000,"timestamp":1409072858,"new":false,"timeSecond":1409072858,"inc":308487737,"machine":-458223042}}

, @Id.

{"name":"Owen","_id":"53fcbedae4b0123e12632639"}

PretendPojo :

   @Test
    public void testJongoIdDeserialization() throws UnknownHostException {
        DB database = new MongoClient("localhost", 27017).getDB("jongo");
        Jongo jongo = new Jongo(database);
        MongoCollection collection = jongo.getCollection("jongo");
        collection.save(new PretendPojo("Owen"));
        PretendPojo pretendPojo = collection.findOne("{name:   \"Owen\"}").as(PretendPojo.class);
        JsonNode json = Json.toJson(pretendPojo);
        assertNotNull(json.get("id"));
    }

, , , //, .

, , :

  {"name":"Owen","id":"53fcbedae4b0123e12632639"}

!:)

+4
4

ObjectIdSerializer , @ObjectId, ObjectId. , .

, NoObjectIdSerializer:

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

:

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

.

+3

@JsonValue, Jongo , , .

@JsonValue
public Map<String, Object> getJson() {
    Map<String, Object> map = new HashMap<>();
    map.put("name", name);
    map.put("id", id);
    return map;
}

@JsonView @Id , Jongo ObjectMapper Jackson ObjectMapper (, REST, )

@Id
@JsonView(Views.DatabaseView.class)
private String id;

@JsonView(Views.PublicView.class)
public String getId() {
    return id;
}
0

Jongo 1,1 .

'_id' , Mongo String, @Id.

@Id + @ObjectId String :

"The My String property with the name" foo "is a valid ObjectId. This property must be stored with the name" _id "and should be treated as ObjectId."

0
source

All Articles