Contract-first Webservice

This tutorial will show you how we can create and publish SOAP based webservice in Contract-first approach using Apache cxf, Spring and Maven. There are mainly two approaches to create the Webservice – Contract-first & Contract-last. The Contract-first approach tells us to create first WSDL and then create end-point interface and implementation class. The Contract-last approach tells us to create first end-point interface and implementation class then create WSDL file.

For this tutorial we will create one maven web project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

The following configurations are required in order to run the application

Eclipse Mars
JDK 1.8(if you use jdk 1.7 then use Tomcat 7)
Tomcat 8
Have maven installed and configured
Apache cxf and Spring dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse

First we will create service project

Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : soap-cxf-spring

Step 2. Modify the pom.xml file as shown below.

Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create an XSD (schema definition file) under src/main/resources/xsd/user.xsd

For more information on XSD please read

In the above XSD we have basically two main elements – getUserDetailsRequest and getUserDetailsResponse. So getUserDetailsRequest will act as an input and getUserDetailsResponse will act as an output based on input.

Step 5. Now create a WSDL (web service definition language) file

In the definition file <wsdl:types/> defines the types of input and output requests, so in this file we have included the external XSD file user.xsd otherwise we had to define the types inside <wsdl:types/>.

In <wsdl:message name=”…”> the name is different from what is declared as element’s name in user.xsd file.

In <wsdl:part element=”…” name=”…”> the name must be same as what is declared as element’s name in user.xsd file.

Then we have <wsdl:portType/>, here the operation name could be as per your choice. The name in <wsdl:operation/> could be anything. The message in <wsdl:input/> or <wsdl:output/> must match with <wsdl:message/> name but the name in <wsdl:input/> or <wsdl:output/> could be anything.

The name in <wsdl:binding/> could be anything but type must match with the name in <wsdl:portType/>. Then inside <wsdl:operation/> we have <wsdl:input/> & <wsdl:output/> and their names must match with the names of <wsdl:input/> and <wsdl:output/> inside <wsdl:portType/>.

The name could be anything for <wsdl:service/>. The binding in <wsdl:port/> must be same as name of <wsdl:binding/>.

Lastly <soap:address/> where host & port have been removed because it will be running in the same server where deployed.

Step 6. Now build the maven project, you will see there are few classes generated in the package com.roytuts.userservice

Step 7. We are not using any database here to fetch data so we will create one mock data class which will give us data based on matching input

Step 8. Maven already generated end-point interface for us, so we will create an implementation class

Step 9. Create spring-beans.xml file inside src/main/resources/spring/spring-beans.xml for configuring beans

In this spring configuration we have also defined the endpoint for webservice.

Step 10. Now complete the web.xml file as below.

Step 11. Noe deploy the project into Tomcat 8 server, you will see the home page coming in the Eclipse default browser.

Step 12. Create a client class in package com.roytuts.userservice.client to test the service

Step 13. Run the client class you will get the output in console as shown below

Thanks for reading.


Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.