JAXB 2 注释和子类令人头痛
我目前在 JAXB 上遇到了一个尴尬的问题。所以我有以下类结构:
@XmlType
public abstract class MySuperClass
{
...
...
@XmlTransient
public Double getValue() ...
...
}
@XmlType
public class MySubClass extends MySuperClass
{
public Double getValue()
{
return 100.00;
}
}
@XmlType
public class MySubClass2 extends MySuperClass
{
public Double getValue()
{
return 100.00;
}
}
现在在我的另一个 JAXB 带注释的类中,我希望这样做:
@XmlType
public class MyOtherClass
{
private MySuperClass var;
public MySuperClass getVar()
{
return this.var;
}
}
所以原因是我想在运行时设置 var ,以便实际返回类型为 MySubClass 或我的子类2。编组到 XML 是完美的,但是,解组回 java 类却给了我空值。未调用 setter 方法。如何正确解组并仍然维护类层次结构?
非常感谢帮助...
谢谢,
I am currently stuck on a awkward problem on JAXB. So I have the following class structure in place:
@XmlType
public abstract class MySuperClass
{
...
...
@XmlTransient
public Double getValue() ...
...
}
@XmlType
public class MySubClass extends MySuperClass
{
public Double getValue()
{
return 100.00;
}
}
@XmlType
public class MySubClass2 extends MySuperClass
{
public Double getValue()
{
return 100.00;
}
}
And now in my other JAXB annotated class, I wish to do this:
@XmlType
public class MyOtherClass
{
private MySuperClass var;
public MySuperClass getVar()
{
return this.var;
}
}
So the reason is that I would like to set var at run time so that the actual return type would be either MySubClass or MySubClass2. Marshalling to XML is flawless, however, unmarshalling back to java classes gave me null values. The setter method wasn't called. How can I unmarshal correctly and still maintain the class hierarchy?
Help is much appreciated ...
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这个问题解决了。看来 JAXB 只在类中寻找声明的 setter 方法,而不是去父类。重写子类中的 setter 方法后,解组工作就开始工作了。
OK, got this one resolved. It seems JAXB is looking for the declared setter method within the class only and not going to the parent class. After overriding the setter method in the subclass, unmarshalling worked.