Web Services with Mule, CXF, and Spring

DZone 's Guide to

Web Services with Mule, CXF, and Spring

· Java Zone ·
Free Resource

Since it seems like everything is going the way of SOA and Web Services these days, it's always nice to have some practical examples of how to quickly get a service up and running. And since many of the frameworks such as Mule and CXF are fairly new to a lot of us, and the documentation tends to take a backseat to the development effort, the more practical examples that are available, the better.

This article is intended to be a quick tutorial on how to get a web service up and running using the combination of a spring-based service (POJO) with CXF and the Mule Enterprise Service Bus.

Getting Started

To run the code associated with this article, you will need to download the latest version of Mule from:

At this time, the latest stable community version is 2.01. Follow the instructions on the website to install Mule.

The Code

For this example, we are going to be creating a Product Catalog service, where users can get a list of the products that we sell, and can also get the details for specific products. We are going to use a code-first approach for this service using JAX-WS annotations.

First, we will need to create several classes:
1) the interface for our service
2) the implementation class for our service
3) our product bean

The Interface

The code for the interface is shown below. Note the annotations that are used. The @WebService annotation is used to denote that this is a web service (pretty self-explanatory), @WebResult is used to specify that our output be wrapped in a tag with the given name, and @WebParam is used to give a name to our input parameter, which makes things more readable for SOAP clients. The rest of the code is just our standard java interface.

package example.catalog;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import java.util.Collection;
import java.util.List;

public interface ProductCatalogService {

public List<String> listProducts();

public Product getProductDetail(@WebParam(name="productId") String productId);


The Implementation

The implementation class is just a regular java class that implements our interface, and uses the single @WebService annotation. Using this annotation, you can specify the name and endpoint interface to be used for the service.

package example.catalog;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebService(endpointInterface = "example.catalog.ProductCatalogService",
serviceName = "ProductCatalogService")
public class ProductCatalogServiceImpl implements ProductCatalogService {
Map<String, Product> productMap = new HashMap<String, Product>();

public ProductCatalogServiceImpl() {
// Load some products
Product product = new Product("SW123", "Square Widget", 10, 10);
productMap.put(product.getId(), product);
product = new Product("RW456", "Round Widget", 5, 5);
productMap.put(product.getId(), product);

public List<String> listProducts() {
List<String> productListing = new ArrayList<String>();

Collection<Product> products = productMap.values();
for (Product p : products) {
productListing.add(p.getId() + " - " + p.getDescription());

return productListing;

public Product getProductDetail(String productId) {
Product product = null;
product = productMap.get(productId);
return product;


The Product Bean

The Product bean is just a standard java bean which must follow standard conventions for everything to work correctly. It must implement the Serializable interface, must have a default constructor, and must have both getters AND setters. This bean will be automatically marshalled to XML using JAXB and will be returned in the SOAP response.

package example.catalog;

import java.io.Serializable;

public class Product implements Serializable {
private String id;
private String description;
private int width;
private int height;

public Product() {

public Product(String id, String description, int width, int height) {
this.id = id;
this.description = description;
this.width = width;
this.height = height;

public String getId() {
return id;

public void setId(String id) {
this.id = id;

public String getDescription() {
return description;

public void setDescription(String description) {
this.description = description;

public int getHeight() {
return height;

public void setHeight(int height) {
this.height = height;

public int getWidth() {
return width;

public void setWidth(int width) {
this.width = width;



In order to get our service to run under Mule, we need to create two configuration files:
1) the Spring config file
2) the Mule config file

The Spring Configuration File

The config file for Spring is shown below. We declare our service implementation bean that will be loaded by the Spring container. We'll save this to a file called catalogContext.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

<bean id="productCatalogService"



The Mule Configuration File

Using the configuration shown below, we first tell Mule the name of our Spring config file. The Mule integration with Spring is excellent.

Next, we setup our service. We declare that the inbound endpoint uses CXF and we define the URL for the service address. We also declare that our Spring-loaded bean is the component that will handle the requests for this service.

We'll save this to a file called catalogservice-config.xml.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.0"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.mulesource.org/schema/mule/core/2.0 http://www.mulesource.org/schema/mule/core/2.0/mule.xsd
http://www.mulesource.org/schema/mule/soap/2.0 http://www.mulesource.org/schema/mule/soap/2.0/mule-soap.xsd
http://www.mulesource.org/schema/mule/cxf/2.0 http://www.mulesource.org/schema/mule/cxf/2.0/mule-cxf.xsd">

<spring:import resource="catalogContext.xml"/>

<model name="services">
<service name="ProductCatalogService">
<cxf:inbound-endpoint address="http://localhost:65082/services/ProductCatalogService" />
<spring-object bean="productCatalogService" />



Build and Deployment

Now that we have our classes and config files created, the next step is to compile the classes and package them up into a jar file. This jar file will then be deployed to the lib/user directory of your Mule installation (i.e. MULE_HOME/lib/user).

Starting the service

To run the service in Mule, you will need to execute the following via command prompt or script:
In Windows: MULE_HOME/bin/mule.bat -config <path-to-config-file>\catalogservice-config.xml
In Unix: MULE_HOME/bin/mule start -config <path-to-config-file>\catalogservice-config.xml

To ensure that the service is running, open up your web browser and go to the following URL:

You should see the WSDL file that has been generated for our service.

Testing the service with SoapUI

Once Mule is up and running with the service, you can easily test it with the free open-source web services testing tool, soapUI. This can be downloaded at:

After launching soapUI, you can create a new project by selecting "New WSDL Project" from the File menu. You will get the following dialog, where you can enter the Project Name and Initial WSDL URL.

Once the project is created, expand the tree, and you should see something like this:


From here, you can double-click on the "Request 1" item under listProducts to open the Request Editor for this operation. Next, just click on the green arrow to call the service. You should see the results as shown below:

You can make the call to the getProductDetail operation in a similar manner. After you open the Request Editor for that operation, just replace the question mark within the <productId> tag with a valid product Id such as SW123. Next, click the green arrow to make the call, and you should get back the SOAP response containing the Product attributes in XML format.

Wrap Up

Using the Mule, CXF, and Spring frameworks, you can quickly get web services up and running. These frameworks give you a lot of flexibility and offer a whole lot more functionality than was covered here. But hopefully this will give a good starting point for further research.



Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}