MuleSoft Technical Guides
Consuming SOAP web service using MuleSoft
This tutorial introduces you to Web Service and creating a high-security consumption of Web Service using SOAP in MuleSoft Anypoint Studio.
What are Web Services?
- Services available over the web are called Web Services.
- Web Services enable communication over the web.
- Provides a standard format/protocol for communication.
- It is a platform-independent communication.
- Using Web Services, two different applications, the first implementation in Java and other implementation on C++, can talk to each other.
For better clarity, we can take the example of ordering food in a restaurant. The waiter acts as a medium between customer and kitchen by conveying the order to chef and getting food delivered over the table. In the same way, a web URL acts as a medium of communication whenever we search or do anything over the Web to get the desired results.
Live Example:
Lets Look at Kayak, an online travel agency and a fine example of web services. When we search for flights on the panel for the flights on desired date and time then our request is redirected to various APIs. All these airlines take the response and check out for flights, their availability and a response is sent back to Kayak from all these airlines. Further, displaying results we get from these APIs in a readable format.
Moving on to SOAP:
SOAP plays a central role in the Service Oriented Architecture (SOA) approach for software design and development
- SOAP stands for Simple Object Access Protocol.
- SOAP is an application communication protocol.
- SOAP is a format for sending and receiving messages.
- SOAP is platform-independent.
- SOAP is based on XML.
- SOAP is a W3C recommendation.
SOAP Message Structure:
WSDL Format:
Advantages of SOAP:
- WS Security: SOAP defines its own security known as WS Security.
- Language and Platform independent: SOAP web services can be written in any programming language and executed in any platform.
Disadvantages of SOAP:
- Slow: SOAP uses XML format that must be parsed to be read. It defines many standards that must be followed while developing the SOAP applications. So it is slow and consumes more bandwidth and resources.
- WSDL dependent: SOAP uses WSDL and doesn’t have any other mechanism to discover the service.
Consuming SOAP through MuleSoft
We are going to consume a SOAP web service and implement different operations with the web service.
[Make sure you have MuleSoft Anypoint Studio 7+ installed on your system.]
Step 1: Make a new project in your Any point Studio by clicking on File>New>Mule Project.
Step 2: Give a Title to the new project. We have named it consuming_soap in this example.
Step 3: Add file module by clicking on add module in the mule pallet as Add Modules>Web Service Consumer.
Step 4: Make the desired flow with the required components shown in the flow below.
Step 5: Set various connectors configuration as below.
You can directly use the XML code from below to set the desired configurations.
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:apikit-soap="http://www.mulesoft.org/schema/mule/apikit-soap" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:wsc="http://www.mulesoft.org/schema/mule/wsc" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd http://www.mulesoft.org/schema/mule/wsc http://www.mulesoft.org/schema/mule/wsc/current/mule-wsc.xsd"> <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="a46b95a3-bc12-4bee-b815-9ee901290d44" > <http:listener-connection host="0.0.0.0" port="8081" /> </http:listener-config> <wsc:config name="Web_Service_Consumer_Config" doc:name="Web Service Consumer Config" doc:id="d2f27227-d308-4984-b881-5dbe05a0a8a8" > <wsc:connection wsdlLocation="http://www.dneonline.com/calculator.asmx?wsdl" service="Calculator" port="CalculatorSoap12" address="http://www.dneonline.com/calculator.asmx"> <wsc:web-service-security actor="http://schemas.xmlsoap.org/soap/actor/next" /> </wsc:connection> </wsc:config> <flow name="soap_serviceFlow" doc:id="81c9f277-86fa-4fd6-b9cf-4f800f4eda87" > <http:listener doc:name="Listener" doc:id="72cb72a0-65ad-4171-92a6-ad1ae121d004" config-ref="HTTP_Listener_config" path="soap"/> <set-variable value="#[attributes.queryParams.Number1]" doc:name="Number1" doc:id="7ed7e420-f957-43ed-ac93-302b5bc44355" variableName="var1" /> <set-variable value="#[attributes.queryParams.Number2]" doc:name="Number2" doc:id="0ac5c0d0-0082-4268-8a51-b2b94f25fdcb" variableName="var2" /> <set-variable value="#[attributes.queryParams.operation]" doc:name="Operation" doc:id="75701e35-3ec6-4c64-b648-b21708cf07c3" variableName="operation"/> <ee:transform doc:name="Transform Message" doc:id="efafe623-01c4-4e3c-93b6-7c9680d29b2b"> <ee:message> <ee:set-payload><![CDATA[%dw 2.0 output application/xml ns ns0 http://tempuri.org/ --- { ns0#Add: { ns0#intA: vars.var1 as Number, ns0#intB: vars.var2 as Number } } ]]></ee:set-payload> </ee:message> </ee:transform> <choice doc:name="Choice" doc:id="9aed1cd0-f11e-44ba-93e1-b95d0a22ffa2" > <when expression="#[vars.operation=='add']"> <wsc:consume doc:name="add" doc:id="3f71cbd8-7c83-432f-9dca-949f9545f77d" config-ref="Web_Service_Consumer_Config" operation="Add"/> </when> <when expression="#[vars.operation=='divide']"> <wsc:consume doc:name="Divide" doc:id="fde17eab-4c99-4242-b3b0-72bf02e48f0a" config-ref="Web_Service_Consumer_Config" operation="Divide"/> </when> <when expression="#[vars.operation=='multiply']"> <wsc:consume doc:name="Multiply" doc:id="18da7485-4651-46cd-8f04-bc9500448e13" config-ref="Web_Service_Consumer_Config" operation="Multiply"/> </when> <when expression="#[vars.operation=='subtract']"> <wsc:consume doc:name="Subtract" doc:id="b8ca06e4-8612-45ee-99ad-4d480785d047" config-ref="Web_Service_Consumer_Config" operation="Subtract"/> </when> <otherwise > <set-payload value="Invalid Operation" doc:name="Set Payload" doc:id="f20c4877-0a1e-40b7-8b8f-b385f1d436bf" /> </otherwise> </choice> </flow> </mule>
Step 6: Hit the URL from postman to see the output we want.
Wrapping Up:
Consuming SOAP web service using MuleSoft is as easy as getting results from the web by searching anything. It is also a high security consumption of web service as we are using SOAP web service. Any MuleSoft Developer can use this flow to consume the SOAP web service using MuleSoft.
Find more MuleSoft best practices here at : https://www.caeliusconsulting.com/blogs/