本文介绍了Moxy不尊重超类/接口属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此输入代码我有一个客户的属性分布在两个接口上,如下所示。我使用子接口ICustomer定义了外部xml绑定。当我将pojo编组为xml时,似乎Moxy忽略了超级界面的属性 firstName 。这是一个错误还是我需要在xml元数据中明确指定这两个接口中的每一个?

enter code hereI have the properties for a Customer spread across two interfaces as shown below. I have the external xml binding defined using the sub interface ICustomer. When I marshall the pojo to xml, it seems like the Moxy is ignoring the super interface's property firstName. Is this a bug or do I need to explicitly specify each of these two interfaces in the xml meta-data?

基础接口

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修复。

Fixed by EclipseLink MOXy V 2.4.1.

我发现超级接口支持添加到JavaClassImpl.getSuperClass方法中如下:

I found that super interface support added into JavaClassImpl.getSuperClass method as following:

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());
}

这篇关于Moxy不尊重超类/接口属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:52