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

  1. Open Anypoint Studio.
  2. Go to File > New > Mule Project.
  3. Project Name: Enter WebServiceConsumerExample.
  4. Click Finish.

Step 2: Add the Web Service Consumer Connector

  1. In the Mule Palette, search for Web Service Consumer.
  2. Drag the Web Service Consumer component onto the Canvas.

Step 3: Configure the Web Service Consumer

Select the WSDL

  1. In the Properties view, click Web Service Consumer to configure it.
  2. Under General, click Add next to Connector Configuration.
  3. 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.
  4. Click OK.

Choose the Operation

  1. 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.

Step 4: Set Up the Input Parameters

  1. 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.
  2. 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:
%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

  1. Drag an HTTP Listener to the Canvas, placing it at the beginning of the flow.
  2. Configure the HTTP Listener:
    • Connector Configuration:
      • Host: 0.0.0.0.
      • Port: 8081.
    • Path: /calculate.

Step 6: Add a Transform Message to Process the Response

  1. After the Web Service Consumer, add another Transform Message component.
  2. 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

  1. Click the Run button in Anypoint Studio.
  2. Ensure the application starts without errors.

Test Using Postman or cURL

Using Postman:

  1. Create a POST request to http://localhost:8081/calculate.
  2. In the Body tab, select raw and choose JSON.
  3. Enter the JSON payload:jsonCopy code{
    "intA": 5,
    "intB": 10
    }

  4. 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.