Schema validation framework that I covered earlier is actually fully supported by
JAXB. If you’re using XFire, you will have to switch to JAXB binding in order to
utilize it.

As described in XFire documentation, you can specify “schema” element as part
of your service definition and point it to your schema. From what I understand,
however, this does not enable validation, it just tells XFire to use
your schema as part of the generated WSDL (in response to “?wsdl” requests).
So I actually had to develop a simple handler to enable it:


public class JaxbValidationEnablingHandler extends AbstractHandler {
   
    public JaxbValidationEnablingHandler() throws SAXException{
        super();
        setPhase(Phase.PARSE);
        before(ReadHeadersHandler.class.getName());
    }
   
    public void invoke(MessageContext ctx) {
        ctx.setProperty(JaxbType.ENABLE_VALIDATION, "true");
    }
}

Another problem that I encountered was that validation exceptions don’t
reach the client, instead the client receives meaningless “Can’t unmarshal” exception.
A simple change in XFire’s JaxbType class fixes that though.

So how does this approach compare to the validation handler approach that I presented
in the previous post? On one hand,
relying on JAXB allows us to easily handle both “wrapped” and “bare” Web service styles.
It also requires minimal effort to implement. On the other hand, very often with Web services
there is a need to be able to process XML without reading it into objects, so, chances are,
JAXB won’t be used in all cases. Also, non-JAXB approach provides for more sophisticated
error handling using
custom error handlers that can be used in conjunction with the validation framework.

Another requirement that have to be considered regardless of the approach is
integration with various XML and Web service registries that are becoming
wide spread in large organizations. This means the ability to read schema from a URL or even
over UDDI or proprietary API.

Clearly, there is still some work to be done in order to make schema validation the norm
for Web services.

8 thoughts on “Using Schema Validation with JAXB and XFire

  1. Hi,

    Nice work. You say that “A simple change in XFire’s JaxbType class fixes that though”. Could you be more precise ?
    Thanks a lot !

  2. Hi!

    Great blog, congratulations!

    But still, I am having a problem with the validation, using the schema that come inside of my web-service wsdl. He loads all the schemas that are imported, but the elements that are declared inside my wsdl, he refuses to find:

    org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ’tis:cancelaGuia’.

    Any help?

    Tanks a lot!

  3. Hey Daniel,

    We are having a very similar problem using XFire validation. And, curiously, we are also working with it on the TISS project! “cancelaGuia”, etc.

    Wonder if maybe we could meet up and exchange some information? You can reach me at mtoledo at amil.com.br

    Cheers!

  4. I moved xsd types to a separate file, and it works.

                                                                                                                                                                                                       
                                                                                                         
                                                                                                                                         
                                                                                                                                                                                                      
        
    

    However I encountered problems with schema validation. Xerces used by xfire recognize
    elements from xsd. So it passes not valid xml files. Any ideas why?

    Regards

  5. OK, I resolved my problem with not validating xs:choices. This was quite tricky. The thing was that XFIRE used a generateSchema method from JAXB’s context if a specific xsd file was not provided. This lead to disappear some parts of xsd.
    So what you need to do is to add explicitly an xsd file, eg. in

    org.codehaus.xfire.spring.remoting.XFireExporter

    <property name=”schemas”>
    <list>
    <value>classpath:yourfile.xsd</value>
    </list>
    </property>

    Regards

  6. Thanks for info and great blog.

    I had trouble with your suggestion with XFire 1.2.6. I kept getting org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ’tns:application’..

    I recorded my experiences on my blog. Basically it was caused by a behaviour that associates first element with wrong namespace. After applying JAXWSProfile, the schema validation started working as shown above.

    Hope this help people out there. Cheers.

Comments are closed.