我的 POJO 是:
import org.jongo.marshall.jackson.id.Id;
public class User {
@Id
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
我从 mongo 数据库中获取用户并希望将他输出到带有 jackson mapper 的文件中
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("c:/user.txt"), user);
我在我的文件中得到了类似的东西
{
"name" : "John",
"age" : 23,
"_id" : {
"time" : 1358443593000,
"inc" : 660831772,
"machine" : 2028353122,
"new" : false,
"timeSecond" : 1358443593
}
}
我需要将 id 字段作为字符串存储到文件中,因为当我反序列化此对象时,pojo 中的 id 字段看起来像这样
{"time":1358443593000,"inc":660831772,"machine":2028353122,"new":false,"timeSecond":1358443593}
任何帮助将不胜感激