Wednesday, 7 October 2015

Web Service Invocation in Mule

Web Service Invocation in Mule

This Tutorial explains how to consume a webservice using mule Web Service Consumer.
Pre-requisites:
  1. Anypoint Studio
  2. Weather Web Service
In the below flow I am setting the city zip code as payload in “Set Payload” Component.
DataMapper component maps this value to the Weather Webservice Operation “GetCityForecastByZip” Request xml Field as “ZIP”.
Webservice Consumer component calls the Weather Web Service to fetch the “GetCityForecastByZipResponse”.
The Next DataMapper Transforms the XML to POJO Object i.e. GetCityForecastByZipResponse (Used for testing purpose only, Can be removed).
Finally GetCityForecastByZipResponse Java Object will be transformed to JSON String and given as response to Http Component.
Which gives the City Weather Information

Main Flow:
Http Component Configuration:

Set Payload Component Configuration:

 DataMapper to prepare Web Service Request:

Web Service Consumer Configuration:

Web Service Configuration:

DataMapper to Transform Webservice Response to POJO Configuration:
 
Main Flow Source:
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:json="http://www.mulesoft.org/schema/mule/json"xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" 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" version="EE-3.5.2"
 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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
<ws:consumer-config name="Web_Service_Consumer" wsdlLocation="Weather.wsdl" service="Weather"port="WeatherSoap12" serviceAddress="http://wsf.cdyne.com/WeatherWS/Weather.asmx" doc:name="Web Service Consumer"/>
<!-- <mulexml:namespace-manager includeConfigNamespaces="true">
<mulexml:namespace prefix="wet" uri="http://ws.cdyne.com/WeatherWS/"/>
</mulexml:namespace-manager> -->
 <configuration doc:name="Configuration">
 <expression-language autoResolveVariables="true">
 <import name="poc" class="com.poc.PocUtil"/>
 <global-functions>
 def changeState(){ return poc.changeState() }
 </global-functions>
 </expression-language>
 </configuration>
<data-mapper:config name="JSON_To_Xml_GetCityWeatherByZIP_"transformationGraphPath="json_to_xml_getcityweatherbyzip_.grf" doc:name="JSON_To_Xml_GetCityWeatherByZIP_"/>
<data-mapper:config name="JSON_To_Xml_GetCityForecastByZIP_"transformationGraphPath="json_to_xml_getcityforecastbyzip_.grf" doc:name="JSON_To_Xml_GetCityForecastByZIP_"/>
<data-mapper:config name="Xml_GetCityForecastByZIPResponse__To_Pojo"transformationGraphPath="xml_getcityforecastbyzipresponse__to_pojo.grf"doc:name="Xml_GetCityForecastByZIPResponse__To_Pojo"/>
<flow name="webservicepocFlow2" doc:name="webservicepocFlow2">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
<set-payload value="{&quot;zipcode&quot;:&quot;00501&quot;}" doc:name="Set Payload"/>
<data-mapper:transform config-ref="JSON_To_Xml_GetCityForecastByZIP_" doc:name="JSON To Xml&lt;GetCityForecastByZIP&gt;"/>
<ws:consumer config-ref="Web_Service_Consumer" operation="GetCityForecastByZIP" doc:name="Web Service Consumer"/>
<data-mapper:transform config-ref="Xml_GetCityForecastByZIPResponse__To_Pojo"doc:name="Xml&lt;GetCityForecastByZIPResponse&gt; To Pojo">
<data-mapper:input-arguments>
</data-mapper:input-arguments>
</data-mapper:transform>
<logger message="#[payload.getCity()]" level="INFO" doc:name="Logger"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
</mule>

7 comments:

  1. Wow… Very informative
    Contact us:
    Locksmith Melbourne,
    Melbourne, VIC-3000,
    Australia.
    Phone: 03 9464 1230
    eMail: info@locksmithmelbourne.net
    Web: https://locksmithmelbourne.net/
    Locksmith Melbourne
    https://locksmithmelbourne.net/

    ReplyDelete
  2. I really like this post…. Well done
    Contact us:
    Locksmith Melbourne,
    Melbourne, VIC-3000,
    Australia.
    Phone: 03 9464 1230
    eMail: info@locksmithmelbourne.net
    Commercial Locksmith Melbourne
    https://www.thomastownlocksmiths.net.au/page/commercial-locksmith/
    Web: https://locksmithmelbourne.net/

    ReplyDelete
  3. Doing great job man… Keep it up
    Contact us:
    Locksmith Melbourne,
    Melbourne, VIC-3000,
    Australia.
    Phone: 03 9464 1230
    eMail: info@locksmithmelbourne.net
    Web: https://locksmithmelbourne.net/
    Automotive Locksmiths Melbourne
    https://www.thomastownlocksmiths.net.au/page/automotive-locksmith/

    ReplyDelete
  4. Very nice… I really like your blog, useful info
    Contact us:
    Locksmith Melbourne,
    Melbourne, VIC-3000,
    Australia.
    Phone: 03 9464 1230
    eMail: info@locksmithmelbourne.net
    Web: https://locksmithmelbourne.net/
    Cheap Locksmith Melbourne
    https://www.thomastownlocksmiths.net.au/

    ReplyDelete
  5. I really like this post…. Well done
    Contact us:
    Locksmith Melbourne,
    Melbourne, VIC-3000,
    Australia.
    Phone: 03 9464 1230
    eMail: info@locksmithmelbourne.net
    Web: https://locksmithmelbourne.net/
    Automotive Locksmiths Melbourne
    https://southmoranglocksmiths.com.au/page/automotive-locksmith/

    ReplyDelete
  6. Wow….. this blog is very niceAwesome…. Your article gives very clear data
    Contact us:
    Locksmith Melbourne,
    Melbourne, VIC-3000,
    Australia.
    Phone: 03 9464 1230
    eMail: info@locksmithmelbourne.net
    Web: https://locksmithmelbourne.net/
    Cheap Locksmith Melbourne
    southmoranglocksmiths.com.au

    ReplyDelete
  7. Nice….. Very useful information
    Contact us:
    Locksmith Melbourne,
    Melbourne, VIC-3000,
    Australia.
    Phone: 03 9464 1230
    eMail: info@locksmithmelbourne.net
    Web: https://locksmithmelbourne.net/
    Cheap Locksmith Melbourne
    southmoranglocksmiths.com.au

    ReplyDelete