Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting JSON from Jersey with circular dependencies

I am writing a service that uses Jersey and JAXB. My classes are annotated with @XMLRootElement, @XMLElement, etc. I have a circular dependency between two classes, so I have annotated the circular dependent property with @XMLTransient. So when I call my service I get xml as the default, which works perfectly. However, when I try using JSON, I get repeated lines like:

{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere"," ... etc.

So it seems there is a problem with circular dependencies when I am using JSON. I would like to avoid the circular dependent item from showing up in the JSON output, like it is done in XML (because of the @XMLTransient annotation).

Can anyone provide any insight on how I would be able to achieve this?


1 Answers

Use @JsonIgnore instead of @XmlTransient to break the circular dependency.

I had a similar problem as you and this did the trick for me.

like image 179
Andy Avatar answered Jan 03 '26 12:01

Andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!