Nav

Lab 2: Connect the Order API to a SOAP Web Service

Overview

In this step we are going to implement the post:/orders​ flow. To create an order, we are going to need to

  1. Call the .NET SOAP Service to create the Order.

  2. Transform the request to XML and response back to JSON format.

Step 1: Add Webservice Connector to the Project

Since we are connecting to a SOAP Service, we are going to use the Webservice consumer.

  1. Click on the Add Module link in the palette.

    module9_lab2_as_wsconnector_add

    An "Add Modules to Project" window will pop up.

    In this window, type web service in the search window to narrow down the module you wish to add.
    Select the Web Service Consumer Module and drag & drop the icon.

    module9_lab2_as_wsconnector_add2

    This will add the Module to your palette, allowing you to select and use the Web Service Consumer processor in your project.

Step 2: Add Consume Component

Now that we have the Webservice component in our project, We are going to consume a Webservice.

  1. Go to post:\orders flow.

Make sure you are modifying the post:/orders flow! Name should look like post:\orders:application\json:api-config or similar.
module9 lab2 as orderflow
  1. Remove the Transform Message component icon. To do that you just right-click on processor icon and select Delete.

    module9 lab2 as deletepp
  2. Let’s add the Web Service Consume processor to the flow. To do that go to the Mule Palette panel and type web service in the filter you should see an icon like in the picture below.

    module9 lab2 as pallette ws
  3. Drag and drop the Consume processor into the empty flow.

    module9 lab2 as wsconnector

    Make sure you put the connector in the Process and not in the Source

Step 3: Configure the WS Connectivity

  1. Double-Click on the Consume processor icon to display its configuration panel. A new pannel will appear.

    module9 lab2 consumer config
  2. Let’s add a new connector configuration. Click on Plus button on the Connector Configuration. Then this window below will pop up.

    module9 lab2 as wscconfig
  3. In the WSDL Location​ field, paste the following WSDL definition, that contains the definitions of our .NET SOAP Service which is already deployed: http://soap-order-ws-v2.us-e1.cloudhub.io/service/BasicHttpBinding_IOrderService?wsdl

    module9 lab2 as wsdl

    Connector automatically populates Services, Port and Address Configuration based on WSLD information. Ensure that service name is shown at Service attribute.

  4. Click the Ok button

Step 4: Configure Consumer Properties

We connected to the service. Now going back to the basic settings, we need to choose the operation and the payload that is going to be sent.

  1. Change the Display Name to Create Order​

  2. Select Create Order​ from the list of operations.

    module9 lab2 as wsdl operation
  3. Go to the Message section.

  4. In the Body section press the module9_lab2_fx button and then press the the module9_lab2_dw button.

    You will see a Dataweave pannel

    module9 lab2 ws dw
  5. Copy and Paste the following script

    %dw 2.0
    output application/xml
    ns ns0 http://tempuri.org/
    var totalPrice = (if(payload.totalPrice == null) sum(payload.orderLineItems.price) else payload.totalPrice as Number)
    ---
    {
    	ns0#CreateOrder: {
    		ns0#Order: {
    			ns0#Items: {
    				(payload.orderLineItems map ( orderLineItem , indexOfOrderLineItem ) -> {
    					ns0#OrderItem: {
    						ns0#ProductId: orderLineItem.productId,
    						ns0#Quantity: orderLineItem.quantitiyOnHand
    					}
    				})
    			},
    			ns0#TotalPrice: totalPrice,
    			ns0#UserId: payload.customerId,
    			ns0#Status: "new"
    		}
    	}
    }

    Pay attention at var totalPrice = (if(payload.totalPrice == null) sum(payload.orderLineItems.price) else payload.totalPrice as Number). Here if the totalPrice doesn’t come in the request, it will be calculated based on the price of each item.

  6. Click the Done button when you are finished.

    module9 lab2 dw done
  7. Click the Save button on the top right of the Properties Panel to save the changes.

Step 5: Build the Response

Now our configuration to create the order is complete, but there is one thing missing, we need to format the message coming in and out of the Create Order ​icon. To do this, we need to add 2 Transform Message processors (DataWeave Elements), one before and one after the Web Service Consumer.

  1. Type transform in the Mule Palette panel

    module9 lab2 as transform
  2. Drag-&-drop a Transform Message after Create Order Web Service

    module9 lab2 as updatedflow
  3. You can see that this one is quite simple, it just returns(output) the id​ of the order just created.

    module9 lab2 as dwmap3
  4. Match the CreateOrderResult​ on the left, with the id​ on the right by dragging the CreateOrderResult field from the input side over to id field on the output side.

  5. After this, just click outside in the white canvas to save the changes, or click the Save button at the top right of the element panel.

Summary

In this lab, you completed the following steps:

Earlier, we used the RAML specification to define our API and auto-generated a skeleton project to implement the API. In this lab we took a specific flow (POST) and implemented a real integration with an external service to Create Order. In doing so we added a Web Service Consumer component, configured it to a WSDL using a valid url. Additionally we transformed the inbound message to do a call to the service, added a Transform component after the Web Service Component to properly handle the response coming back from the web service call. Finally we are now able to run this project, in the next lab, locally on Anypoint Studio to test out the new implementation.

Congratulations! You have completed Lab 2.

Please proceed to Lab 3