我正在使用 wsdl
文件中的 CXF
(wsdl2java) 生成类,但是一个枚举只映射到 String
。
如果我打开生成的类,这是 wsdl 片段:
<complexType>
<complexContent>
<restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
<attribute name="Type" use="required">
<simpleType>
<restriction base="{http://www.w3.org/2001/XMLSchema}string">
<enumeration value="AAA"/>
<enumeration value="VVV"/>
</restriction>
</simpleType>
</attribute>
</restriction>
</complexContent>
</complexType>
为什么结果是 String
而不是 Enum
?
这是自动生成的结果:
private String type;
public String getType() {
return type;
}
public void setType(String value) {
this.type = value;
}
更新:自定义绑定(bind)文件:
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
jaxb:version="2.1">
<jaxb:bindings>
<jaxb:bindings node="//xs:attribute[@name='Type']/xs:simpleType">
<jaxb:typesafeEnumClass ref="TestEnum" />
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
最佳答案
"... one enum is instead mapped to a String only."
"Why is the result a String, and not an Enum?"
当您的枚举值之一不是有效的 Java 标识符(例如:以数字开头)时,可能会发生这种情况 - JAXB(cxf 的 wsdl2java
委托(delegate)工作)事实上被迫使用 String
字段,否则生成的代码将无法编译。
关于java - CXF 不生成枚举映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23540869/