繁体   English   中英

如果Jackson决定使用ObjectIdentity作为输出,则更改JSON属性名称

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM