Is there a possibility to force XJC not to generate Map to List of Key-Value pairs conversion?
When I take Jaxb annotated class
@XmlRootElement
public class Customer {
private Map<String, Address> addressMap = new HashMap<String, Address>();
public Map<String, Address> getAddressMap() {
return addressMap;
}
public void setAddressMap(Map<String, Address> addressMap) {
this.addressMap = addressMap;
}
}
And generate schema via schemagen I will get following xsd schema:
<xs:element name="customer" type="customer"/>
<xs:complexType name="address">
<xs:sequence>
<xs:element name="street" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="addressMap">
<xs:complexType>
<xs:sequence>
<xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="key" minOccurs="0" type="xs:string"/>
<xs:element name="value" minOccurs="0" type="address"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
When I generate jaxb annotated classes back via xjc with specified bindings:
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.1">
<jaxb:bindings schemaLocation="schema1.xsd">
<jaxb:bindings node="//xs:complexType[@name='customer']//xs:element[@name='addressMap']">
<jaxb:property>
<jaxb:baseType name="java.util.HashMap<String,Address>" />
</jaxb:property>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
I will get back:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {
"addressMap"
})
public class Customer {
@XmlElement(required = true)
@XmlJavaTypeAdapter(MapAdapter.class)
protected HashMap<String, Address> addressMap;
public HashMap<String, Address> getAddressMap() {
return addressMap;
}
public void setAddressMap(HashMap<String, Address> value) {
this.addressMap = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"entry"
})
public static class AddressMap {
protected List<Customer.AddressMap.Entry> entry;
public List<Customer.AddressMap.Entry> getEntry() {
if (entry == null) {
entry = new ArrayList<Customer.AddressMap.Entry>();
}
return this.entry;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"key",
"value"
})
public static class Entry {
protected java.lang.String key;
protected blog.map.generated.Address value;
public java.lang.String getKey() {
return key;
}
public void setKey(java.lang.String value) {
this.key = value;
}
public blog.map.generated.Address getValue() {
return value;
}
public void setValue(blog.map.generated.Address value) {
this.value = value;
}
}
}
}
That's not what I exactly wanted. Is there a way to get my "original" input?
There isn't a standard JAXB (JSR-222) way to generate the following class from an XML schema.
@XmlRootElement
public class Customer {
private Map<String, Address> addressMap = new HashMap<String, Address>();
public Map<String, Address> getAddressMap() {
return addressMap;
}
public void setAddressMap(Map<String, Address> addressMap) {
this.addressMap = addressMap;
}
}