I have a pair of objects like
public class Obj1 {
public int id;
public String name;
public Obj2 obj2;
}
public class Obj2 {
public int id;
public String name;
public List<Obj1> obj1list;
}
I want to be able to convert this to Json via Jackson. I found the JsonManagedReference
and JsonBackReference
and annotated them but when you do that, the serialization only works in one way. It will only show when the class with the JsonManagedReference
side is serialized.
If I serialize an "Obj1" I want to see the "Obj2" that is attached to it. And if I serialize the "Obj2" I want to see the list of "Obj1"s that is attached to it.
I also tried using JsonIdentityInfo annotation like so
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
and this seems to work except that it adds the "id" value of the parent object into the subobject (or every subobject in the list case) which is a bit odd. Though I guess I can live with it.
Is there a way to get this to behave as I expect?
See Question&Answers more detail:os