How to use Web-Service-Consumer connector in Mule Community Edition

This post describes how you can use “Web Service Consumer” component without “DataWeaver” or “DataMapper”.

Image Courtesy: Pexels

Web Service Consumer connector in Mule is designed to consume a SOAP web service and retrieve data from external parties. But since this connector is often used with DataWeaver or DataMapper components, which are only available in the Mule Enterprise Edition(EE), there’s a misconception that this can be only used with Mule Enterprise Edition(EE).

Therefore this post is written to show you, how you can still use this component in Community Edition(CE) to consume SOAP web services.

Step 1 :

I’ll be using “Global Weather” public webservice to find out all major cities by country name. Location to the WSDL schema can be found in http://www.webservicex.com/globalweather.asmx?WSDL

Our goal here is to get a list of cities as response from the above web service just by calling an endpoint, passing country name as a query parameter.

For example, calling http://localhost:8085/cities?countryname=Sri Lanka” should internally generate the following SOAP request (Figure 1) and get the relevant response.

Figure 1: SOAP Request

For the generation of the above request we will be using Mule “XSLT Transformer” component. So without further ado let’s get started!

Step 2

First I’ll show you how the message flow would look like in Mule so that you get a feel for it.

Figure2: Mule Message Flow

First component, which is the http listener, will be listening to the requests at the endpoint “http://localhost:8085/cities”. Now XSLT Transformer component requires a valid XML document as the payload. Since we are not setting any payload when calling the http listener, we need to use the “Set payload” component to set a dummy xml. (However, this dummy XML will not be used by XSLT Transformer).

Next important thing we should do is to create the WeatherRequest.xslt (Figure3) file, under src/main/resources folder to generate the SOAP request and populate it with the query parameter value.

Figure 3: WeatherRequest.xslt

If you look closely at the above XSLT file, you will notice that we have used only body/operation part of the SOAP request to pass to the Web Service Consumer component, since it is designed to accept only the body part of the SOAP request.

Parameter name “countryName” will be accessed through the context-property of the XSLT transformer and we can get the value for it from the following Mule Expression.

Figure 4

Then, you can configure the Web Service Consumer component (Figure 5) by giving out the WSDL location and it will automatically fill out the service, port and address for you as shown in the figure below.

Figure 5

Finally, mention the operation that you want to use. In this case I have used “GetCitiesByCountry” operation.

Following is the complete Mule XML configuration file.

Figure 6: consume_soap_ws.xml

So that’s it. Deploy your mule application to your Mule server and you are ready to make requests to your API (Figure 7).

Figure 7: Output