gpt4 book ai didi

java - MOXY 中的 JAXB 继承

转载 作者:数据小太阳 更新时间:2023-10-29 02:11:16 27 4
gpt4 key购买 nike

我有两个类:

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 但它在编码期间打印以下警告消息:

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

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

提前致谢!

编辑:

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

最佳答案

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


选项 #1 - 映射属于父级的继承属性(property)

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

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 的文件,其中包含以下条目。

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 - 映射属于 child 的继承属性(property)

父类 A' 可以被标记@XmlTransient这将允许我们将继承的字段/属性映射到子类B`(参见 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>

关于java - MOXY 中的 JAXB 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10874711/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com