MOXY中的JAXB继承 [英] JAXB inheritance in MOXY

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

问题描述

我有两个课程:

package a;
class A {
 private <type> fieldOfClassA;
 // getters, and setters
}

package b;
class B extends A{
 private <type> fieldOfClassB;
 // getters, and setters
}

我想编组B班到一个xml元素并从类A添加属性fieldOfClassB和fieldOfClassA但它在编组期间打印以下警告消息:

I want to marshal class B to an xml element and add the attribute fieldOfClassB, and fieldOfClassA from class A but it prints the following warning message during marshalling:

Ignoring attribute [fieldOfClassA] on class [b.B] as no Property was generated for it.

请注意,这两个类来自两个不同的包,我无法更改此对象模型。

Note that the two class is from two different packages and I can't change this object model.

提前谢谢!

编辑:

我正在使用外部绑定文件。

I am using external binding files.

推荐答案

从您发布的日志消息中,我可以看到您正在使用MOXy的外部映射文件(参见 http://blog.bdoughan.com/2010/ 12 /延伸-JAXB-表示-annotations.html )。映射继承属性有几种不同的方法。

From the log message you posted, I can see that you are using MOXy's external mapping document (see http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html). There are a couple of different ways to map the inherited property.

选项#1 - 映射继承归属于父级的属性

默认情况下,字段/属性需要映射到它所属的类中。由于MOXy在包级别范围内定义外部映射文档,因此您需要单独的映射文档 A B

By default a field/property needs to be mapped within the class it belongs to. Since MOXy scopes the external mapping document at the package level, you will need separate mapping documents for A and B.

forum10874711 / a / binding1.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum10874711.a">
    <java-types>
        <java-type name="A">
            <java-attributes>
                <xml-element java-attribute="fieldOfClassA" name="field-of-class-a"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

forum10874711 / b / binding1.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum10874711.b">
    <java-types>
        <java-type name="B">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="fieldOfClassB" name="field-of-class-b"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

forum10874711 / b / jaxb.properties

要将MOXy指定为JAXB实现,您需要在与域模型相同的包中添加名为 jaxb.properties 的文件,并带有以下条目。

To specify MOXy as your JAXB implementation you need to add a file called jaxb.properties in the same package as your domain model with the following entry.

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示

package forum10874711;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

import forum10874711.b.B;

public class Demo1 {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        List<String> metadata = new ArrayList<String>(2);
        metadata.add("forum10874711/a/binding1.xml");
        metadata.add("forum10874711/b/binding1.xml");
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadata);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {B.class}, properties);

        B b = new B();
        b.setFieldOfClassA("foo");
        b.setFieldOfClassB(123);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(b, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<b>
   <field-of-class-a>foo</field-of-class-a>
   <field-of-class-b>123</field-of-class-b>
</b>






选项#2 - 映射属于孩子的继承属性

父类 A'可以标记为 @XmlTransient 这将允许我们映射子类 B`上的继承字段/属性(参见 http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html

The parent class A' can be marked@XmlTransientthis will allow us to map the inherited fields/properties on the child classB` (see http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html).

forum10874711 / a / binding2.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum10874711.a">
    <java-types>
        <java-type name="A" xml-transient="true"/>
    </java-types>
</xml-bindings>

forum10874711 / b / binding2.xml

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum10874711.b">
    <java-types>
        <java-type name="B">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="fieldOfClassA" name="field-of-class-a"/>
                <xml-element java-attribute="fieldOfClassB" name="field-of-class-b"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

演示

package forum10874711;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

import forum10874711.b.B;

public class Demo2 {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        List<String> metadata = new ArrayList<String>(2);
        metadata.add("forum10874711/a/binding2.xml");
        metadata.add("forum10874711/b/binding2.xml");
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadata);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {B.class}, properties);

        B b = new B();
        b.setFieldOfClassA("foo");
        b.setFieldOfClassB(123);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(b, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<b>
   <field-of-class-a>foo</field-of-class-a>
   <field-of-class-b>123</field-of-class-b>
</b>

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

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