Reputation: 419
I'm designing a very simple web app with a REST web service that utilizes JPA to interact with a PostgreSQL database and runs in TomEE. My JPA entities have bidirectional mappings and I want my REST service to consume/produce those JPA entities as XML and JSON.
XML serialization works fine because I'm using the @XmlTransient annotation on one side of each bidirectional mapping in order to prevent an infinite loop during serialization.
Unfortunately, during JSON serialization I enter an infinite loop and a StackOverflowError is generated. I assumed that since TomEE uses Apache CXF that it would also use Jettison and I thought Jettison respected the @XmlTransient annotation.
However, it looks like TomEE is actually using Johnzon and that doesn't seem to respect the @XmlTransient annotation. How can I tell Johnzon to ignore certain fields? Could I somehow use the @JsonbTransient annotation from the JSON-B spec? I'd prefer not to link against Johnzon but I tried that in order to use the @JohnzonIgnore annotation without effect. Am I better off forcing TomEE to use Jettison? Any suggestions?
You can reproduce this bug for yourself because the rest-example that TomEE posted on their web site has the same issue, http://tomee.apache.org/examples-trunk/rest-example/README.html.
Upvotes: 2
Views: 417
Reputation: 730
First you can use @javax.json.bind.annotation.JsonbVisibility to switch to fields.
Johnzon also supports cyclic references by enabling 'johnzon.deduplicateObjects'. It basically replaces any cyclic objeccts with JsonPointers and automatically back on deserialisation. A detailed description can be found in the JavaDocs https://github.com/apache/johnzon/blob/master/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java#L486
Upvotes: 1