Using Schema Validation with JAXB and XFire
October 5th, 2006Schema 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.
Hi,
Nice work. You say that “A simple change in XFire’s JaxbType class fixes that though”. Could you be more precise ?
Posted by olivier November 21st, 2006 at 12:32 pmThanks a lot !
Hello, sure. I just created an entry in XFire Jira for that, it has all the details:
http://jira.codehaus.org/browse/XFIRE-771
Let me know if you still have questions.
Alexander.
Posted by Alexander Ananiev November 22nd, 2006 at 12:46 amIt works great. Thank you again. “Vive l’open source !”
Posted by olivier November 22nd, 2006 at 5:06 amHi!
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!
Posted by Daniel February 7th, 2007 at 2:24 pmHey 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!
Posted by Marcos Toledo February 14th, 2007 at 3:47 pmI moved xsd types to a separate file, and it works.
[code]
[/code]
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
Posted by Rafal Rusin February 27th, 2007 at 9:37 amOK, 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
Posted by Rafal Rusin February 27th, 2007 at 3:16 pmThanks 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.
Posted by Ahrum Song June 19th, 2007 at 9:16 am