有几种方法。第一个是使用
@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以外的属性。结果不会包装为对象。



