How to comsume web service in Mulesoft
In this article, I am going to explain how we can consume web service in Mulesoft with example.
Lets start with the HTTP connector first as listener and our flow will be like as in the follwoing image:
Step1:
Step2: In HTTP connector configuration we need to set only the port number.
Step3: You need to set path for http listener as well.
Step4: variable used here in the flow is used to get value from query parameter.
Step5: Then we have used transform message component to set the request data for web service we are going to use.
First drag and drop transform message then click on define metadata and select the operation for which you are setting the request data.
For this please have a look at following screenshots.
Payload for webservice:
Web Service Consumer configuration will be like as following :
After configuring Web service consumer you need to select the operation and you are Done.
Now run your project when its deployed successfully use following url to call this service.
Call this service from postman as :
http://localhost:8081/soap/temp?temprature=30
Thanks!!
XML Configuration File:
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:ws="http://www.mulesoft.org/schema/mule/ws"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.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/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
<ws:consumer-config name="Web_Service_Consumer" wsdlLocation="https://www.w3schools.com/xml/tempconvert.asmx?WSDL" service="TempConvert" port="TempConvertSoap" serviceAddress="http://www.w3schools.com/xml/tempconvert.asmx" doc:name="Web Service Consumer" />
<flow name="consumepublicsoapserviceFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/soap/temp" doc:name="HTTP" />
<set-variable variableName="inputTemp" value="#[message.inboundProperties.'http.query.params'.temperature]" doc:name="Variable" />
<dw:transform-message doc:name="Transform Message">
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/xml
%namespace ns0 https://www.w3schools.com/xml/
---
{
ns0#CelsiusToFahrenheit: flowVars.inputTemp
}]]>
</dw:set-payload>
</dw:transform-message>
<ws:consumer config-ref="Web_Service_Consumer" operation="CelsiusToFahrenheit" doc:name="Web Service Consumer" />
</flow>
</mule>
<mule
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:ws="http://www.mulesoft.org/schema/mule/ws"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.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/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
<ws:consumer-config name="Web_Service_Consumer" wsdlLocation="https://www.w3schools.com/xml/tempconvert.asmx?WSDL" service="TempConvert" port="TempConvertSoap" serviceAddress="http://www.w3schools.com/xml/tempconvert.asmx" doc:name="Web Service Consumer" />
<flow name="consumepublicsoapserviceFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/soap/temp" doc:name="HTTP" />
<set-variable variableName="inputTemp" value="#[message.inboundProperties.'http.query.params'.temperature]" doc:name="Variable" />
<dw:transform-message doc:name="Transform Message">
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/xml
%namespace ns0 https://www.w3schools.com/xml/
---
{
ns0#CelsiusToFahrenheit: flowVars.inputTemp
}]]>
</dw:set-payload>
</dw:transform-message>
<ws:consumer config-ref="Web_Service_Consumer" operation="CelsiusToFahrenheit" doc:name="Web Service Consumer" />
</flow>
</mule>
Comments
Post a Comment