如何在JAXRS上建立父子关系

时间:2013-08-08 11:51:16

标签: java rest jax-rs java-ee-6

我的REST服务出现问题。 问题是我的实体之间存在关系,当这个实体将被编写为JSON时,JBoss无法做到这一点。 以下是代码:

儿童班:

  @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;
    }
  }

父类:

  @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方法

  @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();
  }

JBoss生成了这个破解的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":

如何忽略Child上的“parent”字段?有没有办法在不重写我的实体的情况下做到这一点,例如“自定义编写者”或类似的东西?

1 个答案:

答案 0 :(得分:1)

好吧,@damo帮我看了正确的事情。 当我使用JBoss时,我看起来JBoss内部使用什么API来使用JAX-RS。

我发现JBoss 7.1.1.Final使用RESTeasy 2.3.2.Final,因此,它在内部使用了jackson-jaxrs版本1.9.2。

通过了解这一点,我导入了我的应用程序提供的那些库并使用了@JsonIgnore注释,因为@XmlTransient在提供XML时有效,但如果生成JSON则API会忽略。

如果您使用JBoss 7.1.1.Final并使用maven,只需将其添加到您的依赖项中:

    <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>

我的Child课程的结果如下:

@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;
  }
}