Search This Blog

Lets go..


Apache CXF - Spring Integration using Netbeans

Author:Md Mahfuj Jia

Now days, technologies are changing very rapidly. In an enterprise application integration is the most challenge able part and it is very much necessary to build application with high scalability, reliability, maintainability and testability. Service oriented architecture (SOA) will give you the flexibility to connect with different types of multiple node or language to your business logic layer. For example, you have developed a login service using SOA and you have exposed your wsdl or wadl that any body can consume login service to their own application for login. Apache CXF is one of the most used service oriented architecture (SOA) framework in enterprise application development. I have developed a sample service using Apache CXF and spring integration module with contact first approach. Contact first approach is the industry standard best practice when developing the web service.

Prerequisites for this tutorial:
1.Clear concept of object oriented programming and service-oriented architecture.
2.Basic knowledge about spring framework and dependency injection (IoC container).
3.Basic object relational mapping (ORM) knowledge is required.

Technologies Used:
1.Apache CXF
2.Spring integration module
6.Apache Tomcat – 8+

Database Table Description:

CREATE TABLE `users` (

  `user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,

  `user_name` varchar(45) NOT NULL,

  `display_name` varchar(45) NOT NULL,

  `email` varchar(45) NOT NULL,

  `bank_uid` varchar(45) NOT NULL,

  `status` varchar(45) NOT NULL,

  `created_by` varchar(45) DEFAULT NULL,

  `create_date` datetime DEFAULT NULL,

  `creator_ip` varchar(45) DEFAULT NULL,

  `updated_by` varchar(45) DEFAULT NULL,

  `update_date` datetime DEFAULT NULL,

  `updator_ip` varchar(45) DEFAULT NULL,

  PRIMARY KEY (`user_id`)


Ø  From this article, we see the basic features and comparison of industry oriented soa architecture frameworks.

Key Positives
Key Concerns
Apache AXIS2
Most Commonly Used, Matured & Stable Web Services Development Framework

Supports Multiple Languages (C++, Java)

Supports both Contract-first & Contract-last Approach

In context of Orchestration & Web Services Transaction (long-running transactions) it supports wide variety of related WS-* specifications:
WS-Atomic Transaction, WS-Business Activity, WS-Coordination, WS-Eventing, WS-Transfer
Compatible with Spring Framework
Comparatively More Code Required/Generated w.r.t. Spring WS/CXF

Is being phased out gradually (mostly by Apache CXF)

It is not fully compliant for JAX-WS JAX-RS
Apache CXF
Most widely used Web Services Standard Now; Improvement over AXIS2, which is now gradually being replaced by Apache CXF

Intuitive & Easy to Use (less coding required as compared to AXIS2)

Clean separation of front-ends, like JAX-WS, from the core code

Fully compliant with JAX-WS, JAX-RS & others

Best Performance across all available framework with minimum computation overhead

Supports wide variety of front-end models

Supports both JAX-WS & JAX-RS (for Restful Services)

Supports JBI & SDO (not supported in AXIS2)

Compatible with Spring Framework
Does not support Orchestration & WS Transactions yet

Does not support WSDL 2.0 yet
Spring WS
Best in terms of supporting Contract-first Web Services Development Approach

Enforces Standards & Best Practices by Framework Constraints (no way out of it & hence limitation as well)

Supports Spring Annotations as well as JAX-WS

Least code from developer’s perspective

Best Aligned with Spring Technology Stack (also similar architectural stack as Spring MVC) including Spring Security
Least number of WS-* Specifications supported (does not fully compliant with JAX-WS)

Spring offers itself as standard & hence other Java-compliant frameworks support better standards support

Only support Contract-first Web Services Development Model
























Image: 1-Image: 7 – Here we see how to generate xsd file to data transformation object (dto). There are two files inside xsd folder. user.xsd is for data contact mapping and another is a external binding xml file which gives you instead of xmlgorgeoan date format. If you want to edit xsd file and regenerate dto, you just delete the jaxBnUser folder inside the JAXB Bindings folder in Netbeans. You will see Generated Sources (jaxb) in Netbeans is also deleted. Now you do step: 2-7 again.

Image: 8-Image: 11 –Apache CXF, Data source and Spring integration configuration file.

Image: 10 –Apache CXF has the flexibility to expose web service both SOAP and Restful.  This one is the cxf configuration file.

Image: 11 -Spring integration setup part. Link endpoint service with business logic layer and object conversion mechanism is configured here. Note that in gateway configuration, there is a service-interface declaration. The main feature of this configuration is that you just declare endpoint-interface according to the endpoint interface package and you do not need the endpoint-interface implementation. You just linked your endpoint-interface to your service layer using chain configuration and setup the object transformation mechanism. Spring integration module is responsible for implementing your end-point interface implementation. Endpoint mapping and request-response object transformation manually is a time consuming issue. I think Apache CXF-Spring integration configuration is a dynamic approach for writing web service.

Image: 12 –Endpoint interface of soap web service.

Image: 13 –Endpoint interface of restful web service.

Image: 14 –Here we see the transformation of objects and UserTransformer class is responsible for this. In this project we have used DozerBeanMapper library to transform dto to service and vice verse. This class talks with image-11 applicationContext-si-cxf.xml file for object transformation.

Image: 15 –Service layer implementation.

Image: 16-Image: 17 –Dao layer implementation.

Image: 18 –Image: 23 –These are our exposed web services and we will test these services using SOAP-UI.

Download Project Source Code

In this tutorial, we have seen that how to write web service using SOA framework in java. Spring web service module is another popular and industry oriented SOA framework. But Spring-WS is comparatively harder than Apache CXF but I am a fan of Spring-WS. In Spring WS you need to explicitly implement endpoint interfaces and Spring-WS support only contact first approach and SOAP based web service. But Apache CXF supports contact first and contact last approach and gives support both Soap and rest based web service.