【发布时间】:2018-10-29 12:50:00
【问题描述】:
我有这样的代码:
import org.codehaus.jackson.map.*;
public class MyPojo {
int id;
public int getId()
{ return this.id; }
public void setId(int id)
{ this.id = id; }
public static void main(String[] args) throws Exception {
MyPojo mp = new MyPojo();
mp.setId(4);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
System.out.println(mapper.writeValueAsString(mp));
}
}
预期效果:
{"MyPojo":{"id":4}}
但我想自定义该名称。我无法用@JsonTypeInfo 标记MyPojo,因为我从图书馆学习这门课。
杰克逊有办法吗?
【问题讨论】:
-
尝试将您的
MyPojo子类化并将注释添加到子类中? -
@Usagi Miyamoto,这是个好主意,但我不确定我们是否可以这样做,因为我们的类是通过 thrift 生成的,我们无法更改签名。