java - How to build Parent and child relationship on JAXRS -
i'm having problem rest service. problem have relationship between entities , when entity going written json jboss can't that. here code:
child class:
@xmlrootelement class child implements serializable { private static final long serialversionuid = -6058827989172575778l; private string childname; private parent parent; public string getchildname() { return childname; } public void setchildname(string childname) { this.childname = childname; } public parent getparent() { return parent; } public void setparent(parent parent) { this.parent = parent; } } parent class:
@xmlrootelement class parent implements serializable { private static final long serialversionuid = -8280071521315889541l; private string parentname; private list<child> childs = new arraylist<child>(); public string getparentname() { return parentname; } public void setparentname(string parentname) { this.parentname = parentname; } public list<child> getchilds() { return childs; } public void setchilds(list<child> childs) { this.childs = childs; } } rest method
@get @path("/test") @produces("application/json") public response test() { final parent parent = new parent(); parent.setparentname("parent name"); child child = new child(); child.setchildname("child name"); child.setparent(parent); parent.getchilds().add(child); responsebuilder rb = response.ok(parent); return rb.build(); } the jboss generates broken json:
{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent":{"parentname":"parent name","childs":[{"childname":"child name","parent": how can ignores "parent" field on child? there way without rewriting entities, such "custom writer" or that?
well, @damo helped me on right thing. using jboss i've looked api jboss uses internally work jax-rs.
i discovered jboss 7.1.1.final uses resteasy 2.3.2.final , because of that, uses internally jackson-jaxrs version 1.9.2.
by knowing this, imported libs provided on application , used @jsonignoreannotation because @xmltransient works when providing xml api ignores if generates json.
if use jboss 7.1.1.final , uses maven add dependency:
<dependency> <groupid>org.codehaus.jackson</groupid> <artifactid>jackson-jaxrs</artifactid> <version>1.9.2</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.jboss.resteasy</groupid> <artifactid>resteasy-jaxrs</artifactid> <version>2.3.2.final</version> <scope>provided</scope> </dependency> the result of child class this:
@xmlrootelement @xmlaccessortype(xmlaccesstype.field) @xmltype(proporder = { "childname" }) class child implements serializable { private static final long serialversionuid = -6058827989172575778l; private string childname; @xmltransient @jsonignore private parent parent; public string getchildname() { return childname; } public void setchildname(string childname) { this.childname = childname; } public parent getparent() { return parent; } public void setparent(parent parent) { this.parent = parent; } }
Comments
Post a Comment