This Tutorial
explains how to consume a webservice using mule Web Service Consumer.
Pre-requisites:
- Anypoint Studio
- 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="{"zipcode":"00501"}"
doc:name="Set
Payload"/>
<data-mapper:transform
config-ref="JSON_To_Xml_GetCityForecastByZIP_"
doc:name="JSON
To Xml<GetCityForecastByZIP>"/>
<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<GetCityForecastByZIPResponse>
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>
|