栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何只序列化Jackson子的ID

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何只序列化Jackson子的ID

有几种方法。第一个是使用

@JsonIgnoreProperties
来删除子级的属性,如下所示:

public class Parent {   @JsonIgnoreProperties({"name", "description" }) // leave "id" and whatever child has   public Child child; // or use for getter or setter}

另一种可能性,如果Child对象始终被序列化为id:

public class Child {    // use value of this property _instead_ of object    @JsonValue    public int id;}

还有一种方法是使用

@JsonIdentityInfo

public class Parent {   @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")   @JsonIdentityReference(alwaysAsId=true) // otherwise first ref as POJO, others as id   public Child child; // or use for getter or setter   // if using 'PropertyGenerator', need to have id as property -- not the only choice   public int id;}

这也适用于序列化,并忽略id以外的属性。结果不会包装为对象。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398167.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号