Developing SOAP Web service using Apache CXF

In last post I walked through the steps for developing a simple RESTFull service using apache CXF.  In this post I will be talking about developing SOAP web service using CXF.

Before moving forward let us understand few of the concepts/elements which makes up a SOAP web service

SOAP or Simple Object Access Protocol 

SOAP is a protocol for exchanging XML-based messages over the network using application protocols like http, smtp, etc as carrier. SOAP message comprises of a SOAP envelope. The envelope can be broken into a header and a body. Header contains context related definitions like security while the body contains actual application data. A typical SOAP message looks like

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="">
    <m:GetStockPrice xmlns:m="">

WSDL or Web Services Description Language

WSDL is a standard based XML Language which is used to describe a web service. A WSDL completely describes what public interface an web service exposes, what parameter it expects, structure of output it returns, location of web service. A WSDL defines a web service as collection of communication end points that are capable of exchanging messages. These communication end points are called ports. Port are composed of two parts.

  1. Contains the public interface exposed by the web service. The interface contains all the methods, parameter needed to invoke them and response structure returned by them.
  2. The second part binds the public interface to network protocol like http. The binding comprises of information like location of the public interface and message format for the service.

SOAP communication styles

There exists two types of communication styles

  1. Document
  2. RPC

The communication style used by SOAP web service is defined in its WSDL.

In the Document style the application data which is part of soap body is sent as XML document. This document can be validated completely by a xml schema which is also part of WSDL. As XML can contain structure as per wish of service developer hence the responsibility of marshaling and unmarshaling xml payload lies at end of provider and consumer code.

In RPC style as the name suggests the consumer invokes the methods of service as if he were invoking a local method. To facilitate this the RPC message consists of list of public interface methods that a consumer can invoke. These methods are listed by names as xml elements.  The method parameters needed by these method forms sub elements of  the method element. The responisibility of marshaling/unmarshaling lies with the web service framework. The framework contains its own marshaling/unmarshaling libraries. RPC style results in tightly coupled code between application code and the web service framework, hence norm is create document style services.

With Key concepts in place let see an example of how to write a soap web service using Apache CXF.

Getting the source code for this tutorial

I have committed the source files for this tutorial in SVN.

You can download the web app from

You can download the client from

Note: Both are ItelliJ maven projects so you can directly import them to your intelliJ IDE or copy over the files manually to other IDEs

Create a struts2 skeleton application to contain your service.

You can use any MVC framework but I prefer struts2 for my own reasons. You can see an example of how to create an empty struts2 application in eclipse using maven here.

Add CXF dependencies 

In your project POM add following dependencies to download CXF jars

       <!-- apache cxf -->



For example let us create a simple book shelf web service. For simplicity let us assume following use case.

  1. Insert a book in book self
  2. Retrieve a book from book shelf by title.

Developing the service

This can be done in two ways Code first and contract first. We will be using the code first approach.

Creating a Service Endpoint Interface (SEI)

Let us create a SEI interface called BookShelfService


import com.aranin.weblog4j.vo.BookVO;

import javax.jws.WebMethod;
import javax.jws.WebService;

public interface BookShelfService {

    public  String insertBook(BookVO bookVO);
    public  BookVO getBook(String title);

If you look at the above SEI you can tell that it is a normal java interface with exception of two annotation

@WebService – This is an annotation JAXWS library. It turns a normal POJO into a webservice. In our case the annotation is placed right above the interface definition and it notifies that BookShelfService is not a normal interface rather an webservice interface or SEI. There are other attributes to this annotation that can completely define the webservice but we will not be using it right now.

@WebMethod – This annotation is optional and is mainly used to provide a name attribute to the public method in wsdl.

Implementing the service. 

Now we have our SEI so let us implement the methods in the interface in our BookShelfServiceImpl


import com.aranin.weblog4j.hashdb.HashDB;
import com.aranin.weblog4j.vo.BookVO;

import javax.jws.WebService;

@WebService(endpointInterface = "",
public class BookShelfServiceImpl implements BookShelfService {
    public String insertBook(BookVO bookVO) {
        return "Book with name : " + bookVO.getBookName() + " is now available on the shelf";  //To change body of implemented methods use File | Settings | File Templates.

    public BookVO getBook(String title) {

        return HashDB.getBook(title);  //To change body of implemented methods use File | Settings | File Templates.

This class is a simple POJO implementing the SEI. Only notable thing here is the @WebService annotation. If you look at it closely we have provided the fully qualified class name of the SEI it implements and name of the webservice.

Data Binding class (BookVO)

package com.aranin.weblog4j.vo;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Book")
public class BookVO implements Serializable {

    private long bookId;
    private String bookName;
    private String author;

    public long getBookId() {
        return bookId;

    public void setBookId(long bucketId) {
        this.bookId = bookId;

    public String getBookName() {
        return bookName;

    public void setBookName(String bookName) {
        this.bookName = bookName;

    public String getAuthor() {
        return author;

    public void setAuthor(String author) { = author;

The only thing to note here is the @XmlRootElement annotation. This annotation is part of JAXB library. CXF uses JAXB as default data binding component. As BookVO needs to be transported as XML during the webservice calls hence it needs to marshalled/unmarshalled by the JAXB engine in the CXF installation. Using @XmlRootElement annotation we help JAXB in mapping the BookVO class to an xml with its name attribute as root element of the xml.

Spring Based Server Bean

What makes CXF a first rate choice as webservice framework is that it publishes its service endpoints via a spring based configuration file. Lets create a  the configuration file and register our service in it. We will name the file as beans.xml and save it in WEB-INF folder of our application

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />	

       address="/bookshelfservice" />


Now to load the beans.xml we simply add following in web.xml


Finally we need to wire spring and CXF through web.xml.

       <display-name>CXF Servlet</display-name>


Note: I have not included loading of Spring ContextLoaderListner. If you create a struts2 application via maven using struts2 starter archetype then spring is downloaded and registered by maven project itself.

Now your webservice is ready. Compile and deploy the application in any servlet container. If everything is good then you can see your wsld on following location


Create your client

There are many tools which can be used to generate client code using the wsdl. To save you further trouble we will utilize CXF’s own front end apis. So let us look at the steps.

  1. Create a simple maven project using IDE of your choice. I am using IntelliJ currently and it is awesome. Lets say name of the project is DemoClient.
  2. Add the CXF dependencies as shown in create skeleton application section.
  3. Since we know what the SEIs and public method and binding objects are. We will create them in the client side to save us trouble. In case there are many such classes we can use tools like wsdl2java etc to generate our code.
  4. Create a stub SEI in exact same package structure as the parent SEI.
  5. Create BookVO in same package structure as the parent BookVO.
  6. The above classes should be exactly same as you have created in the parent application.
  7. We need not create the SEI implementation at client end.
  8. Now we will create a client using JaxWsProxyFactoryBean. This class is a factory which works with the SEI proxies to invoke web service methods. Here is the class.

package com.aranin.weblog4j.client;

import com.aranin.weblog4j.vo.BookVO;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class DemoClient {
    public static void main(String[] args){
        String serviceUrl = "http://localhost:8080/weblog4jdemo/bookshelfservice";
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		BookShelfService bookService = (BookShelfService) factory.create();

        //insert book
        BookVO bookVO = new BookVO();
        bookVO.setAuthor("Issac Asimov");
        bookVO.setBookName("Foundation and Earth");

        String result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        bookVO = new BookVO();
        bookVO.setAuthor("Issac Asimov");
        bookVO.setBookName("Foundation and Empire");

        result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        bookVO = new BookVO();
        bookVO.setAuthor("Arthur C Clarke");
        bookVO.setBookName("Rama Revealed");

        result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        //retrieve book

        bookVO = bookService.getBook("Foundation and Earth");

        System.out.println("book name : " + bookVO.getBookName());
        System.out.println("book author : " + bookVO.getAuthor());


Here is the output of above calls

INFO: Creating Service {}BookShelfServiceService from class
result : Book with name : Foundation and Earth is now available on the shelf
result : Book with name : Foundation and Empire is now available on the shelf
result : Book with name : Rama Revealed is now available on the shelf
book name : Foundation and Earth
book author : Issac Asimov

Process finished with exit code 0

There are tons of other stuff you can explore in Apache CXF like Creating dynamic clients, interceptors, leveraging other transport protocol, webservice over https etc. But I intend this post as getting started tutorial.

Phew this is a long post again. I need to improve my writing skills to shorten the length. But still I hope that you enjoyed it and found it useful. I intend to write about a javascript client for webservices in my next post. Until then goodbye and happy coding.

Print Friendly, PDF & Email

About Niraj Singh

I am CEO and CoFounder of a startup "Aranin Software Private Limited, Bangalore. I completed my graduation in 2002 as an Aerospace Engineer from IIT Kharagpur. I love working on new ideas and projects and recently released my first open source project JaiomServer "". I have 9 years of experience in IT industries most of which I have spent in developing community applications for various clients using java. Some of the sites in which I have actively involved with are,,,, etc.
This entry was posted in Apache CXF, web service and tagged , . Bookmark the permalink.

12 Responses to Developing SOAP Web service using Apache CXF

  1. I am not normally big on leaving comments but have you considered installing SEOPressor to improve your on-site Seo?Your weblog looks like it could use some assistance rising up the google standings. You should check it out here:SEOPressor

  2. Mark Man says:

    Hi I was wondering if you’re up for selling your site? Please get in touch whenever you read this. Cheers. Mark

  3. Daytime says:

    would you rent me ad space for my banners??

  4. John Doe says:


    first of all thanks for this great tutorial. I tried to update the cxf libraries to the actual version but everything higher than 2.5.x isn’t running with this example. Can you share an updated version? Looking forward to see the solution.


  5. Niraj Singh says:

    Hi John,

    Sorry for late reply, I normally don’t get comments so I seldom check them on the blog. Anyways, I just tried to deploy the web-service with CXF version 2.7.1 and it works without a glitch. Can you tell me what errors you see in your log? Perhaps we can fix it together. Just for you I have commited the latest files in SVN.


  6. Let me straight away comprehension your current rss while i can’t to locate your current e-mail ongoing url and also e-zine service. Have every? You need to let us recognize so that I really could register. Many thanks.

  7. Howdy very nice blog!! Man .. Excellent .. Wonderful .. I’ll bookmark your web site and take the feeds also?I am satisfied to search out numerous helpful info here in the submit, we’d like develop extra strategies in this regard, thanks for sharing. . . . . .

  8. Terrific get the job done! Option kind of facts that are meant to be contributed through the world wide web. Waste on yahoo for not positioning that publish larger! Occur more than and discuss with my website. Many thanks Means)

  9. reset says:

    You made several nice points there. I did a search on the topic and found the majority of folks will consent with your blog.

  10. That is very interesting, You’re an excessively skilled blogger. I’ve joined your feed and stay up for searching for more of your fantastic post. Also, I have shared your web site in my social networks

Comments are closed.