JAXB-Eclipselink:继承的属性

发布于 2024-12-25 03:35:51 字数 714 浏览 1 评论 0原文

我有以下使用 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 技术交流群。

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

发布评论

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

评论(1

悲凉≈ 2025-01-01 03:35:51

您不需要做任何特别的事情:

B

我已经根据您的之前的问题,以便填充 x 属性:

package forum8739246;

public class B extends A {

    private Foo x;

    public B() {
        x = new Foo();
    }

    public Foo getX() {
        return this.x;
    }

}

oxm.xml

下面是我所基于的元数据文件您对我原来的答案的评论。

<?xml version="1.0"?>
<xml-bindings  
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    version="2.3"
    package-name="forum8739246">
    <java-types>
        <java-type name="B" xml-accessor-type="FIELD">
            <java-attributes>
                <xml-element java-attribute="x" name="X"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

演示

package forum8739246;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.*;
import javax.xml.namespace.QName;

import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8739246/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {C.class},properties);
        System.out.println(jc.getClass());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        JAXBElement<B> b = new JAXBElement<B>(new QName("b"), B.class, new B());
        marshaller.marshal(b, System.out);

        JAXBElement<C> c = new JAXBElement<C>(new QName("c"), C.class, new C());
        marshaller.marshal(c, System.out);
    }

}

输出

从输出中可以看出,x 属性针对 BC 实例进行了编组:

class org.eclipse.persistence.jaxb.JAXBContext
<?xml version="1.0" encoding="UTF-8"?>
<b>
   <X/>
</b>
<?xml version="1.0" encoding="UTF-8"?>
<c>
   <X/>
</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 the x property:

package forum8739246;

public class B extends A {

    private Foo x;

    public B() {
        x = new Foo();
    }

    public Foo getX() {
        return this.x;
    }

}

oxm.xml

Below is the metadata file that I based on your comments to my original answer.

<?xml version="1.0"?>
<xml-bindings  
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    version="2.3"
    package-name="forum8739246">
    <java-types>
        <java-type name="B" xml-accessor-type="FIELD">
            <java-attributes>
                <xml-element java-attribute="x" name="X"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Demo

package forum8739246;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.*;
import javax.xml.namespace.QName;

import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8739246/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {C.class},properties);
        System.out.println(jc.getClass());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        JAXBElement<B> b = new JAXBElement<B>(new QName("b"), B.class, new B());
        marshaller.marshal(b, System.out);

        JAXBElement<C> c = new JAXBElement<C>(new QName("c"), C.class, new C());
        marshaller.marshal(c, System.out);
    }

}

Output

As can be seen from the output the x property is marshalled for both instances of B and C:

class org.eclipse.persistence.jaxb.JAXBContext
<?xml version="1.0" encoding="UTF-8"?>
<b>
   <X/>
</b>
<?xml version="1.0" encoding="UTF-8"?>
<c>
   <X/>
</c>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文