1

我正在为将在 WSDL 中使用的常见 Web 服务类型创建一个 XSD。我需要的一种常见类型是枚举。

我的问题是当我执行 wsimport 生成的工件是一个类而不是一个枚举。

我正在使用 Eclipse Indigo 的 XSD 和 WSDL 编辑器。这就是我在设计模式下创建枚举的方法:

  1. 创建新的复杂类型 (ResponseCodeType)
  2. 在 ResponseCodeType 中添加新的字符串元素(代码)
  3. 在代码的约束属性中,我添加了以下约束值:SUCCESS、WARNING、ERROR、FATAL

我究竟做错了什么?

XSD 源

<complexType name="ResponseCodeType">
    <sequence>
        <element name="code">
            <simpleType>
                <restriction base="string">
                    <enumeration value="SUCCESS"></enumeration>
                    <enumeration value="WARNING"></enumeration>
                    <enumeration value="ERROR"></enumeration>
                    <enumeration value="FATAL"></enumeration>
                </restriction>
            </simpleType>
        </element>
    </sequence>
</complexType>

wsimport 生成的工件的 Java 源代码

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ResponseCodeType", propOrder = {
    "code"
})
public class ResponseCodeType {

    @XmlElement(required = true)
    protected String code;

    /**
     * Gets the value of the code property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getCode() {
        return code;
    }

    /**
     * Sets the value of the code property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setCode(String value) {
        this.code = value;
    }

}
4

1 回答 1

2

我想到了。当我尝试设计我的枚举时,我创建了一个复杂类型,其元素具有我需要的约束(成功、信息、警告等)。

我所做的是创建一个简单的类型,其中包含一个具有约束 (ResponseCode) 的字符串元素。然后我创建了一个带有 ResponseCode 元素的复杂类型(ResponseCodeType)。

当我执行 wsimport 时,它生成 ResponseCode 作为枚举和具有 ResponseCode 属性的 ResponseCodeType 类。

如果有人有更好的方法,请发表评论或提供更好的答案。

于 2012-09-18T21:21:35.243 回答