Web Services with Mule, CXF, and Spring


Configuration

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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="productCatalogService"
class="example.catalog.ProductCatalogServiceImpl"
scope="singleton">
</bean>

</beans>

 

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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:soap="http://www.mulesource.org/schema/mule/soap/2.0"
xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.0"
xsi:schemaLocation="
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:beans>
<spring:import resource="catalogContext.xml"/>
</spring:beans>

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

</mule>

 

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:
http://localhost:65082/services/ProductCatalogService?wsdl

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:
http://www.soapui.org/

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.

 

Article Type: 
How-to
Article Resources: 
0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jason Whaley replied on Fri, 2008/07/04 - 4:28pm

Decent article. I work day to day with a suite of web services implemented with the exact same stack (but using a wsdl first approach).  It, for the most part, gets the job done, although there was more legwork involved than what you've described due to the fact a wsdl first approach was selected.

However, I think this article lacks in showing the real strength of the mule esb, which is applying transformations, multiple/chainable routing, and abstracting the protocol away from the implementation (e.g. seamlessly going from a web service call over HTTP to a JMS queue, yet your code never knows that's happening), and in general sitting in front or behind the web service doing such work and not being solely there for containing the web service.

ryang replied on Thu, 2008/09/18 - 11:28am

Excellent article. The author covered everything what he said at the beginning and mentioned that there were more to explore at the conclusion.  Will someone cover the advanced/other features?

Hat off to Steve Haskam.

 

bgulla replied on Sun, 2009/10/18 - 10:35pm

 Awesome.

I'm learing mule, I impressed with your blog, If you any mule related blogs please let me know. 

Thanks,

Balu

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.