Getting JSON from Jersey with Circle 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 annotated the circular dependent property with @XMLTransient. Therefore, when I call my service, I get xml as the default, which works fine. However, when I try to use JSON, I get duplicate lines, for example:

{"name": "der", "record": [{"" points ": 0," victory ": 0," loss ": 0," ties ": 0," leaders ": {" name ":" der "," record ": [{" points ": 0," victory ": 0," loss ": 0," ties ": 0," leaders ": {" name ":" der "," record ": [ {"points": 0, "wins": 0, "loss": 0, "ties": 0, "full size": {"name": "der", "records": [{"" point ": 0, "wins": 0, "losses": 0, "ties": 0, "leaderboard": {"name": "dere", "... etc.

So it seems that there is a circular dependency problem when I use JSON. I would like the cyclic dependent element to be displayed in the JSON output, as is done in XML (due to @XMLTransient annotation).

Can someone give an idea of ​​how I can achieve this?

+2
source share
2 answers

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

I had the same problem as you and it helped me.

+1
source

It might be worth checking out the Kris Zyp JSON Link to the offer . It was specifically designed to handle multiple links and circular links in JSON data.

(Note. Despite the title of the article, Dojo is not required. The original offer was json.com , but this site is not available to me at the moment.)

How could you implement this technique in Jersey, unfortunately, the exercise remains for the reader.

0
source

Source: https://habr.com/ru/post/1336743/


All Articles