JAXB - XJC设置不生成Map to Key-Value对转换位直接绑定

时间:2013-11-18 16:06:18

标签: jaxb xjc schemagen

是否有可能强制XJC不生成Map to Key-Value对转换?

当我拿Jaxb注释类

@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;
}

}

通过schemagen生成模式我会得到以下xsd模式:

  <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>

当我使用指定的绑定通过xjc生成jaxb带注释的类时:

    <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&lt;String,Address&gt;" />
     </jaxb:property>
     </jaxb:bindings>
     </jaxb:bindings>

  </jaxb:bindings>

我会回来的:

    @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;
            }
        }
    }
}

这不是我想要的。有没有办法获得我的“原创”输入?

1 个答案:

答案 0 :(得分:0)

没有标准JAXB (JSR-222)方法从XML架构生成以下类。

@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;
    }

}