使用Jongo和MongoDB在Jackson反序列化中将ObjectId _id重命名为id [英] Rename ObjectId _id to id in jackson deserialization with Jongo and MongoDB
问题描述
我刚刚开始使用play框架,jongo和MongoDB开发项目。该项目最初是在Play 2.1中用pojos编写的,其字符串ID字段用以下两个注释:@Id和@ObjectId这将作为ObjectId持久保存到Mongo,并且当反序列化时将id输出为:id:53fcb9ede4b0b18314098d10例如。
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 annotated with both: @Id and @ObjectId This would persist to Mongo as an ObjectId and when deserialized would output the id as: "id":"53fcb9ede4b0b18314098d10" for example.
自从升级到Jongo 1.1和Play 2.3.3后,id属性在反序列化时始终被命名为_id,我希望该属性保留字段名称但我可以'使用@JsonProperty(custom_name)作为Jongo @Id注释在幕后执行@JsonProperty(_ id)。
Since upgrading to Jongo 1.1 and Play 2.3.3 the id attribute is always named "_id" when deserialized, I want the attribute to retain the field name yet I can't use @JsonProperty("custom_name") as the Jongo @Id annotation does @JsonProperty("_id") behind the scenes.
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;
}
}
在MongoDB中保留的POJO如果我是这样的话通过RoboMongo查看它们
The POJOs when persisted in MongoDB look like this if I view them via RoboMongo
{
"_id" : ObjectId("53fc984de4b0c34f1905b8ee"),
"name" : "Owen"
}
但是当我反序列化时如果我保留两个注释,我会得到以下json:
However when I deserialize them I get the following json if I keep both annotations:
{"name":"Owen","_id":{"time":1409072858000,"date":1409072858000,"timestamp":1409072858,"new":false,"timeSecond":1409072858,"inc":308487737,"machine":-458223042}}
以及以下输出,如果我只使用@Id注释。
and the following output if I only use the @Id annotation.
{"name":"Owen","_id":"53fcbedae4b0123e12632639"}
我有一个测试用例,用于处理上面的PretendPojo节目:
I have a test case for working with the PretendPojo show above:
@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"));
}
当尝试使用自定义反序列化器时,我永远无法获得对象ID I似乎只能访问当前正在反序列化的日期/时间/时间戳数据。
When trying to use custom deserializers I never can get hold of the object ID I seem to only have access to the date/time/timestamp data that is currently being deserialized.
理想情况下,我正在寻找的输出是:
Ideally the output I'm looking for would be:
{"name":"Owen","id":"53fcbedae4b0123e12632639"}
任何帮助将不胜感激! :)
Any help will be greatly appreciated! :)
推荐答案
ObjectIdSerializer 总是将使用@ObjectId映射的属性写入ObjectId的新实例。将此属性映射到String时出错。
ObjectIdSerializer always writes property mapped with @ObjectId to a new instance of ObjectId. This is wrong when you map this property to a String.
为了避免这种情况,我编写了NoObjectIdSerializer:
To avoid this behaviour, I've write a 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;
这篇关于使用Jongo和MongoDB在Jackson反序列化中将ObjectId _id重命名为id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!