Search code examples
xsdwsimport

How do create enum with Eclipse XSD editor


I'm creating an XSD for common web service types that will be used in WSDLs. One of the common types I need is an enum.

My problem is when I execute wsimport the artifact generated is a class not an enum.

I'm using Eclipse Indigo's XSD and WSDL editor. This is what I do in design mode to create my enum:

  1. Create new Complex Type (ResponseCodeType)
  2. Add new string element (code) in ResponseCodeType
  3. In the constraints property of code, I add the following constraint values: SUCCESS, WARNING, ERROR, FATAL

What am I doing wrong?

XSD source

<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>

Java source for artifact produced by wsimport

@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;
    }

}

Solution

  • I figured it out. When I tried designing my enum I created a complex type with an element having the constraints I needed (SUCCESS, INFO, WARN, ect).

    What I did instead was to create a simple type with a string element having the constraints (ResponseCode). Then I created a complex type (ResponseCodeType) with an element of ResponseCode.

    When I executed wsimport, it generated ResponseCode as an enum and ResponseCodeType class with a ResponseCode attribute.

    If anyone has a better approch please comment or provide a better answer.