Search code examples
javaweb-servicesenumsjax-wsjbossws

Prevent Enum in Java XML Web Services


I love Enum for the type safety and also it makes code much more readable. I always make use of Enum whenever I get the chance.

Problem started when I need to expose these codes as web services. For example, if I have an enum like this:

public enum Language {
    ENGLISH(1),
    BAHASA_MALAYSIA(2);
}

It will be exposed in wsdl as this:

<xs:simpleType name="language">
<xs:restriction base="xs:string">
  <xs:enumeration value="ENGLISH"/>
  <xs:enumeration value="BAHASA_MALAYSIA"/>
  </xs:restriction>
</xs:simpleType>

If in the future if I decided to add a new language I will be in trouble, the wsdl file will be different and it will break older clients.

My question, how do I prevent the enum be exposed as enum in wsdl? I want it be exposed as simple datatype either String or int.

I am using JBoss WS if that matter.


Solution

  • If in the future if I decided to add a new language I will be in trouble, the wsdl file will > be different and it will break older clients.

    No, it won't break older (well constructed) clients. Adding values to an enumaration can be considered a backwards compatible change. See articles like Extensibility, XML Vocabularies, and XML Schema.

    From what your telling I'm guessing your working code first and let JBoss-WS generate the WSDL and XSD. There is nothing wrong with that. However if backward and foreward compatibilty is such a big concern to you you should work contract first (ie. design the WSDL and XSD manually). Because you never know what WSDL's and XSD's newer version of JBossWS are going to look like.

    If you really want JBoss-WS to generate something different then an enumeration you need to look into JAX-B. JAX-B is what the actual XSD generation does.