Json and Java - Circular Reference
A single annotation @JsonIdentityInfo
solves the problem. It handles circular references also.Reference
There are two ways you can go about this. If you must expose your entity to the outside world, I recommend adding @JsonIgnore
on the property that is causing the circular reference. This will tell Jackson not to serialize that property.
Another way is to use the bidirectional features provided by Jackson. You can either use @JsonManagedReference
or @JsonBackReference
. @JsonManagedReference
is the "forward" part of the property and it will get serialized normally. @JsonBackReference
is the "back" part of the reference; it will not be serialized, but will be reconstructed when the "forward" type is deserialized.
You can check out the examples here.
This addresses your comment: I think what you might want to do in this case is use a DTO that is visible to the outside world. I like this approach because I don't want to expose my entities to the outside. This means that the Jackson annotations would be on the DTO and not on the enity. You would need some sort of mapper or converter that converts the entity to the DTO. Now when you make changes to your entity, they won't get propagated to the DTO unless you modify your mapper/converter. I think this is ok, because when you make a change to your entity you can decide if you want that change to be exposed or not.
UPDATE
There is a good blog post here that goes into detail about the various ways you can handle bidirectional relationships in Jackson. It describes solutions that use @JsonIgnore
, @JsonManagedReference
and @JsonBackReference
, @JsonIdentityInfo
, @JsonView
and a custom serializer as well. It's a pretty comprehensive writeup of the various techniques that you can use.