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"}
!:)