MuleSoft Web Service Consumer Example: A Step-by-Step Guide
Integrating with web services is a common requirement in enterprise applications. MuleSoft provides a powerful Web Service Consumer connector that simplifies the process of consuming SOAP web services. This guide walks you through creating a Mule application that consumes a SOAP web service using the Web Service Consumer connector in Anypoint Studio.
Prerequisites
- Anypoint Studio: Installed and set up on your machine.
- Basic Knowledge of MuleSoft: Familiarity with Mule flows and components.
- Access to a SOAP Web Service: For this example, we'll use a publicly available SOAP service.
Step 1: Create a New Mule Project
- Open Anypoint Studio.
- Go to File > New > Mule Project.
- Project Name: Enter
WebServiceConsumerExample
. - Click Finish.
Step 2: Add the Web Service Consumer Connector
- In the Mule Palette, search for Web Service Consumer.
- Drag the Web Service Consumer component onto the Canvas.
Step 3: Configure the Web Service Consumer
Select the WSDL
- In the Properties view, click Web Service Consumer to configure it.
- Under General, click Add next to Connector Configuration.
- In the Web Service Consumer Configuration window:
- WSDL Location: Enter the URL of the WSDL file. For this example, use http://www.dneonline.com/calculator.asmx?WSDL.
- Click OK.
Choose the Operation
- Back in the Properties view:
- Service: Select
Calculator
. - Port: Select
CalculatorSoap
. - Address: The endpoint address should populate automatically.
- Operation: Choose an operation, e.g.,
Add
.
- Service: Select
Step 4: Set Up the Input Parameters
- Add a Transform Message component before the Web Service Consumer:
- Drag Transform Message from the Palette onto the Canvas, connecting it before the Web Service Consumer.
- In the Transform Message component:
- Set the output metadata to match the expected input of the web service operation.
- Click Define Metadata (the gear icon).
- Choose Type: Select CalculatorSoap > Add > Request.
- In the Script area, map the input parameters:
- Set the output metadata to match the expected input of the web service operation.
%dw 2.0
output application/xml
---
{
Add: {
intA: payload.intA as Number,
intB: payload.intB as Number
}
}
3. This script assumes that the incoming payload contains intA
and intB
fields.
Step 5: Expose an HTTP Endpoint for Testing
- Drag an HTTP Listener to the Canvas, placing it at the beginning of the flow.
- Configure the HTTP Listener:
- Connector Configuration:
- Host:
0.0.0.0
. - Port:
8081
.
- Host:
- Path:
/calculate
.
- Connector Configuration:
Step 6: Add a Transform Message to Process the Response
- After the Web Service Consumer, add another Transform Message component.
- Configure it to extract the result from the SOAP response:
- Set the output to JSON or plain text.
- Use the following DataWeave script:
%dw 2.0
output application/json
---
{
result: payload.AddResponse.AddResult
}
Step 7: Complete Flow Overview
Your flow should look like this:
plaintextCopy code[HTTP Listener] --> [Transform Message (Request)] --> [Web Service Consumer] --> [Transform Message (Response)]
Step 8: Run and Test the Application
Run the Application
- Click the Run button in Anypoint Studio.
- Ensure the application starts without errors.
Test Using Postman or cURL
Using Postman:
- Create a POST request to
http://localhost:8081/calculate
. - In the Body tab, select raw and choose JSON.
- Enter the JSON payload:jsonCopy code
{
"intA": 5,
"intB": 10
} - Send the request.
Using cURL:
bashCopy codecurl -X POST -H "Content-Type: application/json" -d '{"intA":5,"intB":10}'
http://localhost:8081/calculate
Verify the Response
- You should receive a JSON response with the result:jsonCopy code
{
"result": 15
}
Conclusion
You've successfully created a Mule application that consumes a SOAP web service using the Web Service Consumer connector. This example demonstrates how to configure the connector, map input parameters, and process the response, providing a foundation for integrating more complex web services.