Java Developing SOAP Web Services with Apache CXF and JiBX

Developing SOAP Web Services with Apache CXF and JiBX

Here we’ll look at developing SOAP Web services using Apache CXF and JiBX. It may be helpful to start with an overview of SOAP Web services architecture, so we’ll begin with the diagram below, plus some SOAP terminology.

SOAP Web Services – Architecture

Advantages of Apache CXF

Apache CXF is currently the most widely used framework, and is preferred over Axis. Here’s why:

  • It separates the JAX-WS code from the core application code

  • It’s compliant with the Spring Framework

  • Apache CXF is more performant than all JAX-WS implementations

  • It supports both JAX-WS and JAX-RS

  • It’s the most popular JAX-WS framework and is replacing all other implementations.

  • And it’s relatively easy to use and generates lesser code, with Eclipse support available

Below is the Apache CXF Services Framework and all components. Especially note the ‘Front-Ends’ and ‘Pluggable Data Bindings’ (source: cxf.apache.org).

We take a deep dive into all things Mazda CX-30 2.5 Turbo and why this crossover has blown our minds.

Advantages of JiBX

JiBX, unlike other binding frameworks, provides a few advantages:

  1. It generates an additional binding class apart from the regular schema, and this ‘separates’ the Java code changes from the binding changes, which means both can be changed independently of each other.

  2. It optimizes the bytecode generated, and is a more performant data binding mechanism by a factor of 2 or more than other binding mechanisms.

  3. It uses various ‘goals’ or ‘phases,’ including Code Generation, Binding Compiler and the Binding Runtime. The number of Java classes and configuration code generated by  JiBX is far greater, an acceptable tradeoff for the performance gain. More on its simple XML binding in the images below (source: jibx.sourceforge.net).

 

CXF/JiBX using Eclipse (Spring / Standalone Server)

You’ll need the following tools and dependencies (with specific versions) to build this sample:

  1. Maven Connector for Eclipse – m2e
  2. CXF Codegen Plugin (Maven Eclipse Connector)
  3. JiBX Maven Plugin  (Maven Eclipse Connector)
  4. Maven Configuration (Spring Dependencies)

Here are the steps to create a sample “Calculator” Web service using CXF and JiBX. We used the following as a reference: https://github.com/FrVaBe/cxf-soap-with-jibx#readme

There are very few pages that explain step-by-step Web service creation. After reading this article on Apache CXF and JiBX, I would recommend it for all JAX-WS/SOAP-based web development in terms of performance. Though I have not done any benchmarking myself, I am impressed with the facts provided for both CXF and JiBX. You can use the below steps to understand (deploy) and then use it directly (as RI) into your project or product.

The only drawback seems to be the curve in understanding (read the ‘depths’) completely both frameworks, which is at times perplexing. Since there are many features that one never uses in a normal Web services project or product, you may be skeptical that you have missed something or there might be something more there. But to start off, I give the detailed steps below. The rest I leave to you. Please share your own comments or code samples below!

Create a Maven Project in Eclipse

I’ll post the full source code of the ‘Maven Project Object Model’ on GitHub shortly, but for now I’m outlining the most important sections here.

 

The properties contain the version for easy reference and changes to the maven build file. The above are the correct version, but you may want to try JiBX 1.2.6+ and also Spring 4.0.0+.  Also, you may want to try this on JDK 1.8.0; we used JDK 1.7.0.

 

The above dependencies are only for Apache CXF and the version is read from the properties section. This contains all the dependencies required for building this project related to CXF.

 

 

The above dependencies are only for JiBX and the version is read from the properties section. This contains ‘all’ the dependencies required for building this project related to CXF.

 

 

The above dependencies are only for Spring. Note that only Spring Context is required for building and running this project. This is true even if you would want to deploy this on a Web/App Server.

Contract-First Web Services (Develop the WSDL/XSD in Eclipse)

I was never a fan of a Contract-First approach. But after using Apache CXF/JiBX, I started believing this is the best way of developing Web services progressively from a requirements and design phase. If you plan early, at the end you may just want to – or have to – copy and paste your specification to generate code using Maven (CXF/JiBX). Otherwise, it will involve some work to create the WSDL/XSD out of your documented service specifications in a Software Requirements Specification or System Design document. You may go through the WSDL/XSD in the attached source code. (Note that the XSD contains the types as normal verb names. The standards recommend that the response/request message names be suffixed with ‘HttpIn’, ‘HttpOut’, ‘Request’, ‘Response’, ‘SoapIn’, ‘SoapOut’.) See the XML schema definition and Web Services Definition Language (WSDL) below:

 

Run the Maven Build (mvn install)

1) generate-sources

The following is the code extract from the pom.xml for the generate-sources from the cxf-codegen-plugin. Also, shown is the declaration (inclusion) of the Maven Compiler for running the Binding Compiler (note the -nexclude flag for Types).

2. generate-java-code-from-schema

This goal is used to generate the Java Types using the JiBX maven plugin. It also generates the binding.xml. All of the generated sources will be available under the ‘target/generated-sources’.

3. bind

The ‘bind’ goal is to bind the generated sources, types and the binding. This phase is the ‘Binding Compiler,’ and when a request is made, then the ‘Binding Runtime’ will kick-in or trigger.

 4. Identify the Generated Classes (Understand CXF/JiBX Better)

Spend some time to identify the generated classes and artifacts to better understand and appreciate the cxf-codegen-plugin and jibx-maven-plugin. Here’s the code for extracting the Binding-Compiler:

5. Deploy and Run (Start with Standalone Java using Spring Context)

You can run the Main class at ‘de.frvabe.sample.calculator.Main’. This will load (and deploy the web services Endpoint) the web services configuration using a Spring ClassPathXmlApplicationContext (from the spring configuration ‘calculator-app-context.xml’).

6. Verify on the Browser (Check if WSDL Exists and is Accessible – Alternatively, use SoapUI)

Find the request provided and the response returned, using SoapUI; see the request and response below:

 

[The function  performed is a ‘dummy add’ with the value supplied (and zero) and the result is returned as response.]

You may choose to run this example on Tomcat or Weblogic or Websphere or JBoss or some other choice. Try adding functionality or exploring other ‘Sophisticated Code Generation’ capabilities of JiBX, as per our project requirement.

You may want to use other reference implementations of JAX-WS or use the default JAXB for developing web services. This article is specifically for SOAP using CXF/JiBX (Eclipse/Standalone/Tomcat). This article may also help people needing an introduction to CXF/JiBX who are already well versed in building SOAP Web Services using any of (or combination of) JAX-WS, Axis, Spring, JAXB and XML.

Latest Posts

Related Stories