gpt4 book ai didi

jaxb - Moxy 不尊重父类(super class)/接口(interface)属性

转载 作者:行者123 更新时间:2023-12-02 02:13:09 25 4
gpt4 key购买 nike

enter code here我有一个客户的属性分布在两个接口(interface)上,如下所示。我使用子接口(interface) ICustomer 定义了外部 xml 绑定(bind)。当我将 pojo 编码为 xml 时,Moxy 似乎忽略了 super 接口(interface)的属性 firstName。这是错误还是我需要在 xml 元数据中明确指定这两个接口(interface)中的每一个?

基础接口(interface)

public interface IBaseCustomer
{
String getFirstName();

void setFirstName(final String firstName);
}

子界面

public interface ICustomer extends IBaseCustomer
{
String getLastName();

void setLastName(final String lastName);

Address getAddress();

void setAddress(final Address address);

List<PhoneNumber> getPhoneNumbers();

void setPhoneNumbers(final List<PhoneNumber> phoneNumbers);

void setPrefix(final String prefix);

String getPrefix();
}

元数据 xml

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="blog.bindingfile">
<xml-schema namespace="http://www.example.com/customer" element-form-default="QUALIFIED" />
<java-types>
<java-type name="ICustomer">
<xml-root-element name="customer"/>
<xml-type prop-order="firstName lastName address phoneNumbers" />
<java-attributes>
<xml-element java-attribute="firstName" name="first-name" />
<xml-element java-attribute="lastName" name="last-name" />
<xml-element java-attribute="phoneNumbers" name="phone-number" />
</java-attributes>
</java-type>
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-value java-attribute="number" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>

输出

<customer xmlns="http://www.example.com/customer">
<prefix>pre</prefix>
</customer>

演示代码

Map<String, Object> properties = new HashMap<String, Object>(1);
InputStream resourceAsStream = Demo.class.getResourceAsStream("xml-bindings.xml");
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, resourceAsStream);
JAXBContext jc = JAXBContext.newInstance("blog.bindingfile", ICustomer.class.getClassLoader(), properties);

ICustomer customer = new Customer();
customer.setPrefix("pre");
customer.setFirstName("firstName");

Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(customer, System.out);

最佳答案

由 EclipseLink MOXy V 2.4.1 修复。

我发现 JavaClassImpl.getSuperClass 方法中添加了 super 接口(interface)支持,如下所示:

public JavaClass getSuperclass() {
if(this.superClassOverride != null) {
return this.superClassOverride;
}
if(jClass.isInterface()) {
Class[] superInterfaces = jClass.getInterfaces();
if(superInterfaces != null) {
if(superInterfaces.length == 1) {
return javaModelImpl.getClass(superInterfaces[0]);
} else {
Class parent = null;
for(Class next:superInterfaces) {
if(!(next.getName().startsWith("java.") || next.getName().startsWith("javax."))) {
if(parent == null) {
parent = next;
} else {
throw JAXBException.invalidInterface(jClass.getName());
}
}
}
return javaModelImpl.getClass(parent);
}
}
}
return javaModelImpl.getClass(jClass.getSuperclass());
}

关于jaxb - Moxy 不尊重父类(super class)/接口(interface)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11853821/

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