Ответ 1
Есть несколько способов. Сначала нужно использовать @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. Однако результат не был бы обернут как Object.