Consuming SOAP Web Services Using Mule
All you need to consume a SOAP-based public web service from Mule Anypoint Studio is a WSDL to locate the SOAP-based web service.
Join the DZone community and get the full member experience.
Join For FreeMule's web service consumer is a ready-made connector that can connect to a SOAP service provider. All it needs is a WSDL to locate the SOAP-based web service.
1. Discover the SOAP Web Service
In order to consume a web service, we need to discover it first. We need to get the WSDL of the web service in order to consume it. For this example, we will use the temperature conversion web service hosted by w3schools. It is public and free to use. You can find it here.
This provides two operations for temperature conversion as shown below:
2. Consume the SOAP Web Service Using Mule Studio
Mule Studio provides HTTP connector for reading the WSDL and extracting the metadata of the web service.
Create a new Mule project. Go to File > New > Mule Project.
Name the project as soap_webservice_consumer
. Click Finish.
On the canvas, drag an HTTP connector as shown below. Click HTTP Connector on the canvas to configure its properties. Below, next to the Console window, you can see HTTP properties window as well.
In Properties window, keep the path as the default / and type Allowed Methods as GET
.
Click the green + sign next to Connector Configuration dropdown. You can see the Global Elements Properties window pop up. Many fields are prepopulated. For now, we use the default host and port.
Click OK. Now drag and drop a web service consumer from the Mule palette onto the canvas. Click Web Service Consumer to configure its properties. Below, you can see the Web Service Consumer properties window.
Click green + sign next to Connect Configuration. This will open a Global Element Properties window as shown below.
Let's set WSDL Location with the value https://www.w3schools.com/xml/tempconvert.asmx?wsdl
.
Service, port, and address are automatically populated by the Web Service Consumer Connector based on the value of WSDL location. If there are multiple services hosted on the same "location," then you can choose from the service drop-down accordingly.
Back to the Mule design. You can see that Connector Configuration is set to the global Web_Service_Consumer element.
Just below that, choose the operation that you want to call. In our case, the TempConvert exposes two operations: CelsiusToFahrenheit and FahrenheitToCelsius. Choose CelsiusToFahrenheit from dropdown as we will need to convert Celsius to Fahrenheit.
Drag and drop a variable element from the Mule palette between HTTP and Web Service Consumer. This will capture the query parameter temp
in the URL and assign it to FlowVar inputTemp
as shown below:
Drag and drop a transform message from the Mule palette, after the variable. Assign inputTemp
to ns0:Celsius: String
as shown below. This will set the input for the SOAP request, which will be sent to the web service.
That's it! Let's send some requests to convert Celsius to Fahrenheit to the SOAP web service. Run the project.
Let's send a request to our mule flow using chrome. For this we will convert 34 degree Celsius to Fahrenheit.
Type localhost:8081/?temp=34 in the address bar and hit enter. We will get the SOAP response with the converted temperature in Fahrenheit.
We have successfully consumed a SOAP-based public web service from Anypoint Studio.
Opinions expressed by DZone contributors are their own.
Comments