JAXB 2 注释和子类令人头痛

发布于 2024-10-08 09:25:47 字数 788 浏览 1 评论 0原文

我目前在 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 技术交流群。

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

发布评论

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

评论(1

醉生梦死 2024-10-15 09:25:47

好的,这个问题解决了。看来 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.

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