它不能爲常量生成唯一的名稱,因爲它試圖用下劃線替換特殊字符(與Java變量命名有關),它留下了重複的變量名。
GMT+9 -> GMT_9
GMT-9 -> GMT_9
因此,存在2個變量具有相同的名稱,這是它失敗。
我們可以強制JAXB使用typesafeEnumMember
使用某些變量名下面的例子來看看
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">
<xsd:simpleType name="UserTimeZoneData">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="UTC" />
<xsd:enumeration value="GMT+9">
<xsd:annotation>
<xsd:appinfo>
<jaxb:typesafeEnumMember name="GMT_PLUS_9" />
</xsd:appinfo>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="GMT-9">
<xsd:annotation>
<xsd:appinfo>
<jaxb:typesafeEnumMember name="GMT_MINUS_9" />
</xsd:appinfo>
</xsd:annotation>
</xsd:enumeration>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
生成的Java文件
UserTimeZoneData.java
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for UserTimeZoneData.
*
* <p>The following schema fragment specifies the expected content contained within this class.
* <p>
* <pre>
* <simpleType name="UserTimeZoneData">
* <restriction base="{http://www.w3.org/2001/XMLSchema}string">
* <enumeration value="UTC"/>
* <enumeration value="GMT+9"/>
* <enumeration value="GMT-9"/>
* </restriction>
* </simpleType>
* </pre>
*
*/
@XmlType(name = "UserTimeZoneData", namespace = "")
@XmlEnum
public enum UserTimeZoneData {
UTC("UTC"),
@XmlEnumValue("GMT+9")
GMT_PLUS_9("GMT+9"),
@XmlEnumValue("GMT-9")
GMT_MINUS_9("GMT-9");
private final String value;
UserTimeZoneData(String v) {
value = v;
}
public String value() {
return value;
}
public static UserTimeZoneData fromValue(String v) {
for (UserTimeZoneData c: UserTimeZoneData.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
參考:https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/Developing_Applications_Using_JAX-WS/files/JAXWSCustomTypeMappingEnum.html
你在使用生成的Java類庫?什麼版本? –
嗨,我們使用jaxb-xjc.jar verion1.0。 –