使用 JAXRS 返回子 ID
使用 @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
今天早上我找到了丢失儿童身份证的解决方案。它实际上不是 JAXRS / JAXB 问题,而是由 Hibernate 映射文件引起的(是的,我仍然喜欢在 Hibernate 的注释上使用映射文件)。
上面示例文件的 Hibernate 映射文件为:
修复方法是强制 Hibernate 不要“延迟加载”子级。我将:更改
为:
使用修改后的休眠映射文件,ID 通过 JAXRS 的结果来获取:
希望如果其他人遇到此问题,这会有所帮助。
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:
The fix was to force Hibernate not to 'lazy load' the children. I changed:
to:
With the modified hibernate mapping files, the IDs came through the results of JAXRS:
Hope this helps if someone else runs into this issue.