[英]Using @JsonAnyGetter /@JsonAnySetter to Serialize JSON and change property name (Jackson)
[英]Change JSON property name if Jackson has decided to use ObjectIdentity for output
上一堂课
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class User {
public int id;
public User other;
}
如果Jackson匹配先前看到的User
对象,它将使用ObjectIdGenerator
输出id而不是实际对象。
输出示例:
{
"id": 1257,
"other" : {
"id": 411,
"other": 1257
}
}
但是,如果对象ID被触发,我想使用其他属性名称,因此输出看起来像
{
"id": 1257,
"other" : {
"id": 411,
"otherRef": 1257
}
}
定制的序列化程序可以实现吗? 我可以找到很多示例,这些示例中的属性值是由自定义序列化程序更改的,而没有一个示例,其中属性名称是根据属性值更改的。
您可以使用@JsonGetter
进行此操作。 用@JsonIgnore
标记该字段,然后创建一个名称为getOtherRef
标有@JsonGetter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.