In this blog, we will look at how to create web services starting from XSD. You will probably start with XSD, when you are using contract first development and defining contract for your web services or have an existing XSD that you would want to use as part of web service development. Lot’s of industry schemas is available as XSD such as ACORD and while developing industry solutions for interoperability, you would typically use XSD approach if a standard schema is available for that industry domain. XSD would provide the input and output message format for web service operations.

We will revisit the sample example of Order process application that I had listed in my blog – CXF and Spring Tutorial . In that blog, I had used the code first approach, where I started with Java code and exposed it as web services.

First we would create the contract for the Order Processing web service. We would name this as Order.xsd. Following shows the listing of Order.xsd. For simplicity, we would use the same Order.xsd schema for input and output operations.

<?xml version=”1.0″ encoding=”UTF-8″?>

<xs:schema attributeFormDefault=”qualified” elementFormDefault=”qualified”

targetNamespace=”http://order.demo/” xmlns:tns=”http://order.demo/”


<xs:complexType name=”order”>


<xs:element minOccurs=”0″ type=”xs:string” />

<xs:element minOccurs=”0″ type=”xs:string” />

<xs:element minOccurs=”0″ type=”xs:double” />

<xs:element minOccurs=”0″ type=”xs:int” />

<xs:element minOccurs=”0″ type=”xs:string” />




Once you have created the contract, there are two alternatives that you have. First alternative is to create a WSDL and include the XSD as part of it and than use the CXF WSDL2Java to create a service implementation. Next approach is to generate Java objects from XSD and used the generated object for input and output operations while creating web services. In this later approach, you don’t need to create a WSDL and instead can start off with Java code and use the generated object as input and output parameter for service methods. We would go with the second approach.

Next we would generate the Java type objects for XML Schema using XMLBeans . XMLBeans is a technology for accessing XML by binding it to Java types. Before that, let’s create the project structure for our web service project. To get started, I have provided the project source code in a zip file. Download the project and unzip it any drive.

The schemas folder contains the Order.xsd and src contains the source code for web service project. The build.xml contained in cxfxsdfirst folder, is the ant build file for building and running the project. The build.xml requires CXF_HOME environment variable to be set to CXF distribution as it uses CXF and dependent library files from CXF_HOME\lib for building and running the code .

Next we would, generate the Java XML schema code for Order.xsd by running the following command.

ant  buildOrderXMLBean

This buildOrderXMLBean uses the org.apache.xmlbeans.impl.tool.XMLBean class to generate Java types for Order.xsd. After running the above command, the generated compiled files would be packaged inside jar file named orderxmlbeans.jar and copied into build folder. The order element defined in Order.xsd would be mapped to the generated Order class. We would use the Order class as input and output parameter for the web service operation.

Next, we would create a service interface and service implementation for Order Process service. The service interface and implementation is exactly the same as Order Process service that was developed in code first approach in my earlier blog, hence I will no got into much details on explaining the code. If you need more details, please refer to my earlier post-CXF and Spring Tutorial

Following shows the service interface. The source code is available in cxfxsdfirst/src/demo/order

package demo.order;

import javax.jws.WebService;


public interface OrderProcess {

Order processOrder(Order order);


Note that the Order class parameter comes from the generated Java types. That’s the only difference from code first approach in terms of service interface and implementation, where we have had created the Order java bean upfront.

The implementation class is Following shows the listing

package demo.order;

import javax.jws.WebService;


public class OrderProcessImpl implements OrderProcess {

public Order processOrder(Order order) {

String orderID = validate(order);


return order;


//Rest of the code commented out. Refer to downloaded code


Next, we would create a Stand-alone server named that would publish the order process server. The following shows the code listing. The code is available in cxfxsdfirst/src/demo/order.

package demo.order;




import org.apache.cxf.databinding.DataBinding;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class Server {

public static void main(String[] args) {

// Service instance

OrderProcess orderProcess = new OrderProcessImpl();

//AddressVerifyProcess orderProcess = new AddressVerifyProcessImpl();

JaxWsServerFactoryBean restServer = new JaxWsServerFactoryBean();



try {


} catch (IllegalAccessException e1) {


} catch (InstantiationException e1) {




BufferedReader br = new BufferedReader(new InputStreamReader(;

try {


} catch (IOException e) {


System.out.println(“Server Stopped”);



The code above uses the JaxWsServerFactoryBean for publishing the Order Process service class over address http://localhost:8080/. The most important code of the Order Process is the following line.


This tells the CFX framework to uses XmlBeans data binding , instead of default JAXB data binding.

Next , we would create a stand-alone client that would invoke the Order Process service. The client and Spring configuration file s similar to one developed for code first. If you need details of the explanation of the code. please refer to Developing a client section in CXF and Spring Tutorial blog.

The client code is available in – cxfxsdfirst/src/demo/order/client folder. Following shows the code listing for

package demo.order.client;

import demo.order.OrderProcess;

import demo.order.Order;

import demo.order.impl.OrderImpl;


public final class Client {

public Client() {


public static void main(String args[]) throws Exception {

ClassPathXmlApplicationContext context

= new ClassPathXmlApplicationContext(new String[] {“demo/order/client/client-beans.xml”});

OrderProcess client = (OrderProcess) context.getBean(“orderClient”);

Order order = Order.Factory.newInstance();





Order orderResponse = client.processOrder(order);

String message = (orderResponse.getId() == null) ? “Order not approved” : “Order approved; order ID is ” + orderResponse.getId();




The following shows the client-beans.xml, the Spring configuration file which is used by

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=””




<jaxws:client id=”orderClient” serviceClass=”demo.order.OrderProcess” address=”http://localhost:8080/orderapp/OrderProcess” >






The following definition tells the CXF framework to use XMLBeans data binding framework.




Next, we would see our code in action. The outline is same as one defined in Running the code section of my earlier blog “ CXF and Spring Tutorial

Navigate to cxfxsdfirst and type in the following command

  • ant build

This would build the source code.

  • ant server

This would execute the Java class – demo.order.Server and publish the OrderProcess service over address http://localhost:8080 .You can see the WSDL for OrderProcess service by typing the URL – http://localhost:8080/OrderProcess?wsdl in the browser

  • ant client

Open up a new window and type the above command. The would execute the Java class – demo.order.client.Client and invoke the OrderProcess service. You would see the order id being displayed in the console.

To know, more about how to develop SOAP and RESTful web services, get a copy of my Apache CXF Web Service Book –

Click this link to buy the book.

Tags : Apache CXF BookCONTRACT-FIRSTCXFtutorialXSD

The author Navveen