Search code examples
cxfjaxb2wsdl2java

CXF wsdl2java: Validation of XML restriction in generated stubs


Question: Using CXFs wsdl2java, is it possible to include restrictions of the schema elements and types in the generated Java stubs?

Use case: if my WSDL includes the following type:

 <xs:complexType name="TestResponse">
  <xs:sequence>
    <xs:element name="code">
      <xs:simpleType>
        <xs:restriction base="xs:int">
          <xs:maxInclusive value="5"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name="text">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:maxLength value="5"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
   </xs:sequence>
 </xs:complexType>

wsdljava by default generates stubs with setters such as:

public void setCode(Integer value) {
    this.code = value;
}

public void setText(String value) {
    this.text = value;
}

where it would be great to have something similar to:

public void setCode(Integer value) {
    if (value > 5) throw new ValidationException();
    this.code = value;
}

public void setText(String value) {
    if (value.length() > 5) throw new ValidationException();
    this.text = value;
}

Is it correct that no such tool/JAXB plugin currently exists for XJC and hence CXFs wsdljava?

What I found out so far:

At least in 2010, according to this post in the Oracle Java Programming forum, it seems that it is not supported out of the box or with any of the available plugins.

I am aware of the following possible alternative approaches to this (but haven't tested them completely):

  • Using the krasa-jaxb-tools JAXB plugin to generate JSR-303 annotations on the stubs as has been discussed on stackoverflow here and here already
  • Dumping XJC and CXFs wsdl2java and using XmlBeans to generate stubs (which for me is not an option since our project relies on CXF heavily)
  • Wait for CXF to validate the stubs once they are serialized as discussed here (however, this means to loose the validation while calling the setters)

Solution

  • I haven't seen an XJC plugin that will do this. If you would like to pursue writing one for some of the basic validation steps, we would certainly appreciate the contribution.

    CXF's wsdl2java does support XMLBeans if you would like to flip to using XMLBeans as the datamodel. Add the "-db xmlbeans" flag to the command line and it should generate XMLBeans stuff instead of JAXB.