This post gives a brief overview on how to build an Apache CXF SOAP service with JiBX databinding.
Apache CXF is an open source services framework that helps building JAX-WS and JAX-RS web services. JiBX is a XML binding tool for Java. To understand the post it is necessary to be familiar with Apache CXF and JiBX.
Apache CXF uses JAXB as its default XML databinding. Thus this combination works out off the box and is well documented. Although JiBX is also supported it seems to be a bit neglected. The support is not even
mentioned in the CXF databinding documentation (to get it there an improvement request was opened CXF-4801).
To demonstrate the usage of Apache CXF with JiBX I built a sample application (simple calculator service). It is accessible on github (cxf-soap-with-jibx) and shows how to build a wsdl first webservice using maven, the cxf-codegen-plugin and the jibx-maven-plugin.
The sample application offers a simple SOAP calculator service with the following definitions
calculator.wsdl service definition
calculator.xsd business objects
I will not paste the file content here as it will make the post unnecessarily long. Instead I will explain the most recent parts of the maven pom.xml that are necessary to build the project.
First of all we use the cxf-codegen-plugin to generate the SOAP service classes. I used it that way:
This will only generate the source code for the service classes. The usage of JiBX is specified in the <dataBinding> element. The cxf-codegen-plugin normally would also generate the classes for the business objects (specified in the calculator.xsd) but it does not allow to pass any specific options to the JiBX code generation process. But that should be done because the business object classes should implement the java.io.Serializable interface. Therefore we need to extend the code generation by using a class-decorator. The customization file custom-jibx-codegen.xml which defines the usage of the class-decorator for all classes of the http://calculator.sample.frvabe.de/types namespace looks like this:
Now the jibx-maven-plugin is used to generate the business object classes:
The schema-codegen goal of this plugin definition will generate the code with the defined customization. The compile-binding goal will compile the generated binding definition into the Java classes (what makes the JiBX XML mapping so fast).
And that's all what has to be done.
In the example Spring is used to start the service with an embedded jetty. The application context xml file is short. Notice that JiBX is also declared as databinding here:
After starting the service you can use e.g. SoapUI to test it with some simple requests, like this:
The result is something like this (please apologize that it ignores most mathematical rules):
Apache CXF and JiBX work well together. But if code generation customization is required there is a lack of support on the Apache CXF tool side for JiBX. Thus some extra work(-around) has to be done.
I opened an improvement request for a better JiBX support CXF-4980. Feel free to vote on this issue!