JAXB-Eclipselink:继承的属性 [英] JAXB-Eclipselink: Inherited properties

查看:133
本文介绍了JAXB-Eclipselink:继承的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用例,用于使用Eclipselink MOXy 2.3将POJO编组为XML:

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

}

我需要编组B和C而不是A.
所以我设置了A是短暂的,这使得B继承了在编组时将被编组的所有成员B.
我不能将B设置为瞬态,因为我需要自己封送它,但是当我编组C时,我需要属性B.getX ()也要编组。

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.

除了 @Override getX()之外还有什么方法可以把它整理好了吗?目前它只是我需要做的一个属性,但想象一个包含许多成员的大型B类,其中一个需要 @Override 在C中编组它们与C一起。

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)?

这里的Eclipselink / JAXB方式是什么?

What is the Eclipselink/JAXB way to go here?

问候,

推荐答案

您无需做任何特别的事情:

There is nothing special you need to do:

B

我根据你的一个 B 类.com / questions / 8727402 / jaxb-eclipselink-mapping-abstract-getter-to-xml>之前的问题,以填充 x 属性:

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>

演示

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属性是为 B 和<$ c的两个实例编组的$ c> C :

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>

这篇关于JAXB-Eclipselink:继承的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆