Contract first CXF

CXF is an apache framework for developing web services. It implements the JAX-WS APIs and claims to be significantly easier to use than the reference implementation. A lot of the information around this framework focuses on developing java-first webservices. However contract-first should offer better stability of the interface so here is a short tutorial for getting a prototype contract-first webservice up and running.

Generate a simple maven project

mvn archetype:generate

  1. Select the default archetype, maven-archetype-quickstart (I couldnt find a specifc archetype for CXF contract-first)
  2. Fill in the parameters:

version = 6
groupId = com.yourcompany.sample
artifactId = sample-ws
version = 1.0-SNAPSHOT
package = com.yourcompany.sample


Add the CXF dependencies to your pom.xml


Add your wsdl

I have created a very simple wsdl, registry.wsdl. It has one operation, retreive, which retreives a registry item based on id. Place the file under /sample-ws/src/main/wsdl/

<?xml version="1.0" encoding="UTF-8" standalone="no"?><wsdl:definitions xmlns:wsdl="" xmlns:sch="" xmlns:soap="" xmlns:tns="" targetNamespace="">
 <xs:schema xmlns:sk="" xmlns:xs="" elementFormDefault="qualified" targetNamespace="">
 <xs:element name="RegistryRequestType">
 <xs:element ref="sk:Id"/>
 <xs:element name="RegistryResponseType">
 <xs:element name="Registry" type="sk:RegistryType"/>
 <xs:complexType name="RegistryType">
 <xs:element ref="sk:Id"/>
 <xs:element ref="sk:Name"/>
 <xs:element name="Id" type="xs:integer"/>
 <xs:element name="Name" type="xs:NCName"/>
 <wsdl:message name="RegistryRequestMsg">
 <wsdl:part element="tns:RegistryRequestType" name="RegistryRequest">
 <wsdl:message name="RegistryResponseMsg">
 <wsdl:part element="tns:RegistryResponseType" name="RegistryResponse">
 <wsdl:portType name="RegistryResource">
 <wsdl:operation name="retreive">
 <wsdl:input message="tns:RegistryRequestMsg" name="RegistryRequest">
 <wsdl:output message="tns:RegistryResponseMsg" name="RegistryResponse">
 <wsdl:binding name="RegistryResourceSoap" type="tns:RegistryResource">
 <soap:binding style="document" transport=""/>
 <wsdl:operation name="retreive">
 <soap:operation />
 <soap:body parts="RegistryRequest"/>
 <soap:body parts="RegistryResponse"/>
 <wsdl:service name="RegistryResourceService">
 <wsdl:port binding="tns:RegistryResourceSoap" name="RegistryResourceSoap">
 <soap:address location="/registryService/"/>

Add the cxf-codegen-plugin

This plugin will be used to generate an implementation stub based on your webservices wsdl. You need fill the wsdl element with the location of your wsdl file. The extraarg elements, -impl and – verbose specify that implementation starting point should be generated and the code generation should be verbose respectively.


Include generated sources

In order to complete the implmentation the generated stubs must be included in the souce of the maven build. Add the generated files by adding the build-helper-maven-plugin referring to the path of the generated files /target/generated/src/main/java.

Note: M2Eclipse doesnt seem to pick up on this source folder so you will have to add it manually in eclipse.


Implement the service

Next move the generated implementation (RegistryResourceImpl) to your normal source folder src/main/java. My naive implementation simple returns a registry object with the same id as the request and the name “Registry Name”

 * Please modify this class to meet your needs
 * This class is not complete


import java.util.logging.Logger;

import com.yourcompany.sample.ns.RegistryRequestType;
import com.yourcompany.sample.ns.RegistryResource;
import com.yourcompany.sample.ns.RegistryResponseType;
import com.yourcompany.sample.ns.RegistryType;

 * This class was generated by Apache CXF 2.3.1
 * Tue Feb 01 14:56:51 CET 2011
 * Generated source version: 2.3.1

 serviceName = "RegistryResourceService",
 portName = "RegistryResourceSoap",
 targetNamespace = "",
 wsdlLocation = "file:/D:/Projects/EFP/samples/sample-ws/src/main/wsdl/registry.wsdl",
 endpointInterface = "com.yourcompany.sample.ns.RegistryResource")

public class RegistryResourceImpl implements RegistryResource {

 private static final Logger LOG = Logger.getLogger(RegistryResourceImpl.class.getName());

 /* (non-Javadoc)
 * @see com.yourcompany.sample.ns.RegistryResource#retreive(com.yourcompany.sample.ns.RegistryRequestType  registryRequest )*
 public RegistryResponseType retreive(RegistryRequestType registryRequest) {"Executing operation retreive");
 try {
 RegistryResponseType registryResponse = new RegistryResponseType();
 RegistryType registryType = new RegistryType();
 registryType.setName("Registry Name");
 return registryResponse;
 } catch (Exception ex) {
 throw new RuntimeException(ex);


Create and deploy war

Next add the web.xml under the expected maven structure src/main/webapp/WEB-INF/web.xml. It declares that the CXFServlet should be responsile for dealing with requests and initialises the spring context.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi=""

 <display-name>Contract-First CXF Web Service</display-name>






The spring application centext should import the cxf beans and define the jaxws endpoint:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 xmlns:xsi="" xmlns:context=""
 xmlns:jee="" xmlns:jaxws=""

 <context:annotation-config />

 <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" />

 <!-- JAX-WS Service Endpoint -->
 <jaxws:endpoint id="registryWS" implementor="#registryResource" address="/registryService" />

 <bean id="registryResource">


Add the maven-war-plugin to the pom.xml and set the output directory to your weblogic profiles autodeploy directory. Add change the packaging to be war.


After that a simple mvn clean install should deploy the webservice on your running weblogic server.Here is a sample request and response:

<soapenv:Envelope xmlns:soapenv="" xmlns:ns="">

<soap:Envelope xmlns:soap="">
 <RegistryResponseType xmlns="">
 <Name>Registry Name</Name>


About bebblebrox

I am an experienced software developer with over five year’s commercial experience with strong software design and development skills. I am searching for an ambitious start-up company with a shared passion for delivering quality software products.
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Contract first CXF

  1. asbestos says:

    Man, you are genius!
    10x for post

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s