enter code here
我擁有客戶跨兩個接口的屬性,如下所示。我有使用子接口ICustomer定義的外部XML綁定。當我將pojo編組爲xml時,似乎Moxy忽略了超級界面的屬性名字。這是一個錯誤,還是我需要在xml元數據中明確指定這兩個接口中的每一個?Moxy不尊重超級類/接口屬性
Base接口
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);
Java接口的成員方法始終是公開。 – 2012-08-08 20:36:20