1

I'm having a problem with my REST service. The problem is that I have a relationship between my entities and when this entity is going to be written as a JSON the JBoss can't do that. Here are the 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 this 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 I ignores the "parent" field on the Child? Is there a way to do that without rewriting my entities, such as a "custom writer" or something like that?

endrigoantonini
  • 1,191
  • 2
  • 15
  • 28
  • 1
    Have you tried marking it with the `@XmlTransient` annotation? – DannyMo Aug 08 '13 at 15:54
  • Not tried to use that, but if I do this, what happens if I try to just returns the entity Child? It will never be exported? – endrigoantonini Aug 08 '13 at 19:11
  • Yes, but if it wasn't transient, you would still have the cyclic reference issue. See [this question](http://stackoverflow.com/questions/3073364/jaxb-mapping-cyclic-references-to-xml) for more information – DannyMo Aug 08 '13 at 19:21
  • Got your point for the help. I'll think what I'll do. Unfortunately the JAXRS doesn't have a thing such as `@XmlInverseReference`. A good link: http://jaxb.java.net/guide/Mapping_cyclic_references_to_XML.html – endrigoantonini Aug 08 '13 at 20:26
  • Well, I tried this. With XML: worked. But with JSON: don't. :( – endrigoantonini Aug 09 '13 at 01:54

1 Answers1

1

Well, @damo helped me to look on the right thing. As I using JBoss I've looked what was the API that JBoss uses internally to work with JAX-RS.

I discovered that JBoss 7.1.1.Final uses the RESTeasy 2.3.2.Final and because of that, it uses internally the jackson-jaxrs version 1.9.2.

By knowing this, I imported those libs as provided on my application and used the @JsonIgnoreannotation just because the @XmlTransient works when you are providing XML but the API ignores if you generates JSON.

If you use JBoss 7.1.1.Final and uses maven just add this to your 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 my Child class is 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;
  }
}
endrigoantonini
  • 1,191
  • 2
  • 15
  • 28