Apache CXF and validation
The ease of development provided by CXF for webservices has been key factor for its popularity amongst developers. Support for various WS-* standards and pluggable extensions are unique features of CXF.
While CXF definitely scores over other frameworks, I recently came across one problem of data validation using Code First approach. The requirement was to perform basic validation on data sent by Webservices client like minimum length, min-max range etc. But somehow the approach that I used was not working as expected and validations couldn’t be forced (I used schema-validation-enabled property). The actual problem cannot be attributed to CXF as the valueobjects were generated using JAXB. The constraints enforced in XML Schema was not reflected in JAXB generated POJO classes. It seems JAXB doesn’t support the XML schema validations.
So I thought of finding any alternative to fix the things and fortunately I found one option (hmm… well there are still some problems). The trick is to use jaxws:schemaLocations property while defining webservice and explicitly specifying schema to be used for webservice. Using this options simply instructs CXF not to generate schema information from JAXB classes and include the contents from schema location. But there are few caveats of this approach
1. You should define type information for request and response.
2. Should include entire schema information in one XSD. Imported and included schema definition doesn’t work for Webservice clients. (Check bug logged by me at CXF issues).
While the validation worked well, the problem no. 2 listed above needs to be resolved by CXF team. Most of the developers tend to break schema definition in multiple files to share it across multiple projects but the bug in CXF simply prevents that.
If you have any other solution, please post it here.



August 3rd, 2010 at 6:21 pm
I don’t work much with Java-first web services, but populating the endpoint wsdlLocation field might also help:
http://stackoverflow.com/questions/2231779/cxf-and-validation-schema-restrictions-ignored
August 5th, 2010 at 12:48 pm
Hi Glen,
With Code-First approach, developers usually dont write WSDL and let the framework take overhead of generating it.
As per your suggestion, specifying wsdlLocation in endpoint would require generating WSDL and I personally feel that it would defeat
the purpose of code-first approach.
If WSDL is created for code-first approach, it would also require developers to keep it upto date with java code, which might be error prone.
Hence the simplest approach would be to use the schemaLocation and specify the XSDs used for JAXB value object generation.
This also saves from efforts spent in maintaining upto date version of WSDL with java code.
Anyways thanks for pointing to the link.