有几种方法。第一个是 用于从子项中删除属性,如下所示:@JsonIgnoreProperties
public class Parent {
@JsonIgnoreProperties({"name", "description" }) // leave "id" and whatever child has
public Child child; // or use for getter or setter
}
另一种可能性,如果子对象始终序列化为 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 以外的属性。但是,结果不会包装为对象。