is it possible to make use of the concept of shared references with JSON as output mode? I read this article http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html), but changing the @Produces on my JAX-RS to JSON forces an endless loop. Basically i want to reduce an object to it`s id:
public class Foo {
private long id;
private String someText;
private Bar bar;
}
I want this to bind instances of this like so:
{
"id": 1234,
"someText": "lorem",
"bar_id": 9876
}
This is what i want to avoid:
{
"id": 1234,
"someText": "lorem",
"bar": {
"id": 9876,
"anotherText": "ipsum"
}
}
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
With MOXy as your JSON-binding provider the shared references post you referenced (from my blog) will work for JSON just as it does for XML. Since you are using JAX-RS below is an example of configuring MOXy in that environment:
When using @XmlID
/@XmlIDREF
it is expected that the object being reference by its ID also exist in the document. Since this isn't your use case you would be better off using an XmlAdapter
. The XmlAdapter
would have a signature similar to:
public class BarAdapter extends XmlAdapter<Integer, Bar> {
...
}
In the XmlAdapter
you would be responsible for returning the id from the instance of Bar
during the marshal, and for returning an instance of Bar
based on the id
during an unmarshal.