问题描述
我正在从 wsdl
文件中生成 CXF
(wsdl2java)的类,但是一个枚举被替代到 String
。
I'm generating classes with CXF
(wsdl2java) out of wsdl
files, but one enum is instead mapped to a String
only.
如果我打开生成的类,这是wsdl代码片段:
If I open the generated class, this is the wsdl snippet:
<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
,而不是枚举
?
这是自动生成的结果:
Why is the result a String
, and not an Enum
?This is the auto generated result:
private String type;
public String getType() {
return type;
}
public void setType(String value) {
this.type = value;
}
更新:自定义绑定文件:
Update: custom binding file:
<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>
推荐答案
以下信息是我的实验结果。我在CXF手册中找不到任何有用的东西。
The following information appeared as a result of my experiments. I couldn't find anything useful in the CXF manuals.
- 如果您在其他类型中声明类型,而
simpletype
,你在容器java类中没有类型,只有一个字段。如果您的限制是基于string
,则您将具有String
类型的字段。 - 如果内部类型是
complextype
(您必须在 simplecontent 元素> complextype 和restriction
),你有一个内部类的正确名称,但它不是一个真正的枚举。您可以通过getValue()
获取字符串值。您可以使用任何字符串数据,并且不会发生错误。 (IMHO,绝对无用的变体) - 如果您将枚举声明为
complextype
而不使用容器类型,则将其作为公开不是内在的类。另外呢,就像以前一样。再次,它不是枚举,不检查正确性,没有真正的限制。 - 如果您在任何容器类型之外声明枚举类型,并且它将是一个
simpletype
,您有一个公共非获奖者列举显然,这是你想要看到的。
- if you declare a type inside another type, and it is
simpletype
, you have NO type in the container java class, but only a field. If your restriction was based onstring
, you will have the field of theString
type. - if that inner type is
complextype
(you must put asimplecontent
element betweencomplextype
andrestriction
), you have an inner class with the correct name, but it is NOT a real enumeration. You can get a String value bygetValue()
. You can use any string data for it and get no error. (IMHO, absolutely useless variant) - if you declare your enumeration as a
complextype
without container type, you will have it as a public not-inner class. Otherwards, it is as the previous. Again, it is no enumeration, no check for correctness, no real restrictions. Useless. - if you declare your enumeration type outside of any container type, and it will be a
simpletype
, you have a public non-inner enumeration. Obviously, it is what you would like to see.
更糟糕的是,即使第四个变体也不会在XML消息给你如果:
What is worse, even the fourth variant won't catch an error in an XML message for you. If:
enumeration StyleType {A,B,C}
...
StyleType Style
而且您的XML消息不正确(不是A,B,C中的一个)Style,您将简单地使用 getStyle()
时为null。所以,而不是有一个很好的消息在...消息在线...位置...有不正确的数据,你必须在每个gerStyle()之后添加一个不为null的检查。如果您不希望用户获得 NullPointerException
的。
And you have XML message with incorrect (not one of A,B,C) value for Style, you will simply get null when using getStyle()
. So, instead of having nice message "in ... message on line ... position ... there are incorrect data", you have to add a check for not null after every gerStyle(). If you don't want the user to get NullPointerException
's.
这篇关于CXF没有生成枚举映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!