使用 JAXRS 返回子 ID

发布于 2024-11-15 11:54:12 字数 2594 浏览 1 评论 0原文

使用 @Produces 时,我遇到了 JAXRS / JAXB 的问题,包括 JSON 结果中的子 ID。以下是我的代码的一部分。由于我们使用 Hibernate,因此我将 id 抽象为 AbstractEntity 类。

POJO:

@XmlRootElement
public abstract class AbstractEntity implements Serializable {
    private Serializable id;

    @XmlElement(type-Object.class)
    @XmlSchemaType(name="anySimpleType")
    public final Serializable getId() {
      return this.id
    }

    public final Serializable setId(Serializable id) {
      this.id = id;
    }
}

@XmlRootElement
public class Parent extends AbstractEntity {
    private String parentName;
    private Child child;

    @XmlElement
    public String getParentName() {
       return parentName;
    }

    @XmlElement
    public Child getChild() {
       return child;
    }

}

@XmlRootElement
public class Child extends AbstractEntity {

    private String childName;

    @XmlElement
    public String getChildName() {
       return childName;
    }

}

JAXRS 服务:(

@Path("/parent")
public class ParentService {

    @GET
    @Path("/get/{id}")
    @Produces(MediaType.APPLICATION.JSON)
    public Parent getById(@PathParam("id") Long id) {

        Parent parent = hibernateDataController.getParentById(id);

        if (parent== null)
           throw new NotFoundException("GET: Parent" + id + " not found.");

        return parent;

     }
}

@Path("/child")
public class ChildService {

    @GET
    @Path("/get/{id}")
    @Produces(MediaType.APPLICATION.JSON)
    public Child getById(@PathParam("id") Long id) {

        Child child = hibernateDataController.getChildById(id);

        if (child == null)
           throw new NotFoundException("GET: Child " + id + " not found.");

        return child;

     }
}

注意:还有更多代码未显示,但主要部分在上面)

我的项目位于 Eclipse 中,使用 Maven,因此我启动了 Jetty: mvn jetty:run

这就是问题开始的地方。我可以使用以下方式访问子 POJO:

http://myserver:8080/example/child/get/1 返回->

{
    "id":{"@type":"xs:long","$":"1"},
    "childName":"Bart Simpson"
}

但是,当我访问父 POJO 时,不会返回子 POJO 的 id:

http:// myserver:8080/example/parent/get/1 返回 ->

{
    "id":{"@type":"xs:long","$":"1"},
    "parentName":"Homer Simpson",
    "child": {
        "childName":"BartSimpson"
    }
}

请注意,未返回子项的 ID,仅返回子项名称。与我合作的 GUI 团队正在使用 GWT,他们要求我在 JSON 结果中包含所有子项的 ID。

如果您能帮助 JAXRS / JAXB 返回子 JSON 中的 ID,我们将不胜感激。感谢您抽出时间。

马特

I am running into an issue with JAXRS / JAXB including child IDs in JSON results when using @Produces. Below are portions of my code. Since we are using Hibernate, I'm abstracting the id into an AbstractEntity class.

POJOs:

@XmlRootElement
public abstract class AbstractEntity implements Serializable {
    private Serializable id;

    @XmlElement(type-Object.class)
    @XmlSchemaType(name="anySimpleType")
    public final Serializable getId() {
      return this.id
    }

    public final Serializable setId(Serializable id) {
      this.id = id;
    }
}

@XmlRootElement
public class Parent extends AbstractEntity {
    private String parentName;
    private Child child;

    @XmlElement
    public String getParentName() {
       return parentName;
    }

    @XmlElement
    public Child getChild() {
       return child;
    }

}

@XmlRootElement
public class Child extends AbstractEntity {

    private String childName;

    @XmlElement
    public String getChildName() {
       return childName;
    }

}

JAXRS Services:

@Path("/parent")
public class ParentService {

    @GET
    @Path("/get/{id}")
    @Produces(MediaType.APPLICATION.JSON)
    public Parent getById(@PathParam("id") Long id) {

        Parent parent = hibernateDataController.getParentById(id);

        if (parent== null)
           throw new NotFoundException("GET: Parent" + id + " not found.");

        return parent;

     }
}

@Path("/child")
public class ChildService {

    @GET
    @Path("/get/{id}")
    @Produces(MediaType.APPLICATION.JSON)
    public Child getById(@PathParam("id") Long id) {

        Child child = hibernateDataController.getChildById(id);

        if (child == null)
           throw new NotFoundException("GET: Child " + id + " not found.");

        return child;

     }
}

(Note: There is more code not shown, but the main parts are above)

My project is in Eclipse, using Maven, so I fire up Jetty: mvn jetty:run

This is where the problem starts. I can access the child POJO using:

http://myserver:8080/example/child/get/1 returns->

{
    "id":{"@type":"xs:long","$":"1"},
    "childName":"Bart Simpson"
}

But, when I access the parent POJO, the id of the child POJO is not returned:

http://myserver:8080/example/parent/get/1 returns ->

{
    "id":{"@type":"xs:long","$":"1"},
    "parentName":"Homer Simpson",
    "child": {
        "childName":"BartSimpson"
    }
}

Notice that the ID of the child is not returned, just the childName. The GUI team that I am working with is using GWT and they are requesting that I include the ID of any children in the JSON results.

Any help in getting JAXRS / JAXB to return the ID within the child JSON would be greatly appreciated. Thanks for your time.

Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沐歌 2024-11-22 11:54:12

今天早上我找到了丢失儿童身份证的解决方案。它实际上不是 JAXRS / JAXB 问题,而是由 Hibernate 映射文件引起的(是的,我仍然喜欢在 Hibernate 的注释上使用映射文件)。

上面示例文件的 Hibernate 映射文件为:

<hibernate-mapping>
    <class name="com.mycompany.Parent" table="PARENT">
    <id name="id" type="java.lang.Long">
        <column name="PARENT_ID" scale="0" />
        <generator class="native" />
    </id>
    <property name="parentName" type="java.lang.String">
        <column name="PARENT_NAME" />
    </property>
    <set name="children" inverse="true" lazy="true" table="CHILD" fetch="select">
        <key>
        <column name="CHILD_ID" />
        </key>
        <one-to-many class="com.mycompany.Child" />
    </set>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="com.mycompany.Child" table="CHILD">
    <id name="id" type="java.lang.Long">
        <column name="CHILD_ID" scale="0" />
        <generator class="native" />
    </id>
    <property name="childName" type="java.lang.String">
        <column name="CHILD_NAME" />
    </property>
    <many-to-one name="parent" type="com.mycompany.Child" fetch="select">
        <column name="PARENT_ID" />
    </many-to-one>
    </class>
</hibernate-mapping>

修复方法是强制 Hibernate 不要“延迟加载”子级。我将:更改

<set name="children" inverse="true" **lazy="true"** table="CHILD" **fetch="select"**>

为:

<set name="children" inverse="true" lazy="false" table="CHILD" **fetch="join"**>

使用修改后的休眠映射文件,ID 通过 JAXRS 的结果来获取:

{
    "id":{"@type":"xs:long","$":"1"},
    "parentName":"Homer Simpson",
    "child": {
        "id":{"@type":"xs:long","$":"1"},
        "childName":"BartSimpson"
    }
}

希望如果其他人遇到此问题,这会有所帮助。

I found my solution to the missing child IDs this morning. It actually was not a JAXRS / JAXB issue, but caused by the Hibernate mapping files (yes, I still like to use a mapping file over annotations for Hibernate).

The Hibernate mapping file for the example file above would be:

<hibernate-mapping>
    <class name="com.mycompany.Parent" table="PARENT">
    <id name="id" type="java.lang.Long">
        <column name="PARENT_ID" scale="0" />
        <generator class="native" />
    </id>
    <property name="parentName" type="java.lang.String">
        <column name="PARENT_NAME" />
    </property>
    <set name="children" inverse="true" lazy="true" table="CHILD" fetch="select">
        <key>
        <column name="CHILD_ID" />
        </key>
        <one-to-many class="com.mycompany.Child" />
    </set>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="com.mycompany.Child" table="CHILD">
    <id name="id" type="java.lang.Long">
        <column name="CHILD_ID" scale="0" />
        <generator class="native" />
    </id>
    <property name="childName" type="java.lang.String">
        <column name="CHILD_NAME" />
    </property>
    <many-to-one name="parent" type="com.mycompany.Child" fetch="select">
        <column name="PARENT_ID" />
    </many-to-one>
    </class>
</hibernate-mapping>

The fix was to force Hibernate not to 'lazy load' the children. I changed:

<set name="children" inverse="true" **lazy="true"** table="CHILD" **fetch="select"**>

to:

<set name="children" inverse="true" lazy="false" table="CHILD" **fetch="join"**>

With the modified hibernate mapping files, the IDs came through the results of JAXRS:

{
    "id":{"@type":"xs:long","$":"1"},
    "parentName":"Homer Simpson",
    "child": {
        "id":{"@type":"xs:long","$":"1"},
        "childName":"BartSimpson"
    }
}

Hope this helps if someone else runs into this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文