I have created module in which I have exposed one REST API which is returning RESPONSE as OpenMRS Person Object. The problem that I am facing is Person has PersonAddress and PersonAddress has Person so cyclic dependency and because of which I am getting Cyclic dependency error while returning the error.
Note: I don’t want to fix anything in the OpenMRS project like using @JsonIdentityInfo, @JsonManagedReference and also I don’t want to convert it into SimpleObect.
It would be great if someone give me some pointer to fix this issue in the module itself.
Thanks in Advance…!!!
First of all, who is throwing the exception? A library you’re using?
A couple of solutions …
Remove cyclic dependencies in the domain entities. For instance, make person in PersonAddress a uuid instead of a class. I don’t think is doable because it requires changing core and probably lots of refactoring.
Do not annotate domain entities, create new classes and annotate these instead. Use a factory to create these resources from domain entities. It’s a bit of work but has advantages: not polluting domain classes and avoid Hibernate exceptions and quirks.
If you’re using a library probably has some mechanisms to avoid this problem, check its documentation. I had similar problems in the past with XML serialization.
+1 to what @lluismf said.
There is no way to automatically convert an OpenMRS Java object to JSON or XML because they naturally have these cyclic dependencies.
That is why the webservices.rest module introduces SimpleObject.
If you want to do something that is consistent with the OpenMRS webservices.rest module, then it’s easy to compose a SimpleObject that contains your desired representation of a person, like is done here.
Alternately, if you prefer to have java classes that can be automatically serialized/deserialized, you need to do Lluis’s second suggestion and copy non-cyclical properties into your new class.
Thanks for the suggestion but yes in #1. I need to modify domain entities of OpenMRS object which I don’t want to do it because tomorrow If I have to upgrade OpenMRS version then I have to fix all this in newer version too.
I am not sure about #2 .Can you explain me in bit details