gpt4 book ai didi

java - 如何在 JAXRS 上建立父子关系

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:40 26 4
gpt4 key购买 nike

我的 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;
}
}

休息方法

  @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":

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

最佳答案

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

关于java - 如何在 JAXRS 上建立父子关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com