mem*_*und 2 java enums wsdl web-services cxf
我正在使用CXF
(wsdl2java)wsdl
文件生成类,但一个枚举被映射到String
only。
如果我打开生成的类,这是 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>
Run Code Online (Sandbox Code Playgroud)
为什么结果是 aString
而不是 an Enum
?这是自动生成的结果:
private String type;
public String getType() {
return type;
}
public void setType(String value) {
this.type = value;
}
Run Code Online (Sandbox Code Playgroud)
更新:自定义绑定文件:
<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>
Run Code Online (Sandbox Code Playgroud)
以下信息是我的实验结果。我在 CXF 手册中找不到任何有用的内容。
simpletype
,那么你在容器java类中没有类型,而只有一个字段。如果您的限制基于string
,您将拥有该String
类型的字段。complextype
放置一个simplecontent
元素),则您有一个具有正确名称的内部类,但它不是真正的枚举。您可以通过 获取 String 值。您可以使用任何字符串数据,并且不会出现错误。(恕我直言,绝对无用的变体)complextype
restriction
getValue()
complextype
无容器类型,则它将作为公共非内部类。其他方面与前一样。同样,它不是枚举,没有正确性检查,没有真正的限制。无用。simpletype
,那么您就有一个公共非内部枚举。显然,这是你愿意看到的。更糟糕的是,即使是第四种变体也无法为您捕获 XML 消息中的错误。如果:
enumeration StyleType {A,B,C}
...
StyleType Style
Run Code Online (Sandbox Code Playgroud)
并且您的 XML 消息的 Style 值不正确(不是 A、B、C 之一),当使用getStyle()
. 因此,您必须在每个 gerStyle() 之后添加一个不为空的检查,而不是有好的消息“在...消息在线...位置...有不正确的数据”。如果你不希望用户得到NullPointerException
's.