JAXB-Eclipselink:继承的属性
我有以下使用 Eclipselink MOXy 2.3 将 POJO 编组为 XML 的用例:
public abstract class A {
public abstract getX();
}
public class B extends A {
private Foo x;
@Override
public Foo getX() {
return this.x;
}
}
public class C extends B {
// Various fields and properties here
}
我需要编组 B 和 C,但不需要编组 A。 所以我将 A 设置为瞬态,这使得 B 继承其在编组 B 时将被编组的所有成员。 我无法将 B 设置为瞬态,因为我需要自行编组它,但是当我编组 C 时,我也需要对属性 B.getX() 进行编组。
除了 C 中的 @Override
getX() 之外,还有其他方法可以对其进行编组吗?目前,这只是我需要执行此操作的一个属性,但想象一下一个具有许多成员的大型 B 类,其中需要在 C 中使用 @Override
将它们与 C 编组在一起
。外部映射文件中是否有任何注释或可能性来标记超类中的属性由其直接子类(或所有子类)继承?
Eclipselink/JAXB 的访问方式是什么?
问候,
I have following use-case for marshalling a POJO to XML using Eclipselink MOXy 2.3:
public abstract class A {
public abstract getX();
}
public class B extends A {
private Foo x;
@Override
public Foo getX() {
return this.x;
}
}
public class C extends B {
// Various fields and properties here
}
I need to marshal B and C but not A.
So i set A to be transient which makes B inherit all its members that will be marshalled when marshalling B.
I cant set B to be transient since i need to marshal it by itself, but when i marshal C, i need property B.getX() to be marshalled as well.
Is there any way other than @Override
getX() in C to have it marshalled? At the moment it is just one property for which i need to do this, but imagine a large B class with many members, which one would need to @Override
in C to marshal them together with C.
Is there any annotation or possibility in the external mapping file to mark a property in a superclass to be inherited by its immediate subclass (or all subclasses)?
What is the Eclipselink/JAXB way to go here?
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要做任何特别的事情:
B
我已经根据您的之前的问题,以便填充
x
属性:oxm.xml
下面是我所基于的元数据文件您对我原来的答案的评论。
演示
输出
从输出中可以看出,x 属性针对
B
和C
实例进行了编组:There is nothing special you need to do:
B
I have modified the
B
class based on one of your previous questions in order to populate thex
property:oxm.xml
Below is the metadata file that I based on your comments to my original answer.
Demo
Output
As can be seen from the output the x property is marshalled for both instances of
B
andC
: