WSO2 - 聚合SOAP和休息响应到休息

时间:2015-04-08 15:41:12

标签: wso2

我正在尝试使用天气肥皂和rest api使用克隆和聚合在Wso2(wso2的新内容,这是我使用Wso2的第3周)中进行概念验证。 方案是客户端将启动soap请求,Wso2过滤器和克隆请求并发送weather soap和rest API GetCityWeatherByZIP操作,两者的响应都在聚合中收集并向客户端发送soap响应。 我的问题是如何将rest响应(jsonObject)转换为soap响应并将其作为子项添加到其他GetCityWeatherByZIP soap响应并返回到客户端,当客户端收到响应时,soap body应该在GetCityWeatherByZIPResponse内部有2GetCityWeatherByZIPResult。

任何帮助都会非常感激。

这是我的代理配置

<proxy xmlns="http://ws.apache.org/ns/synapse" name="CloneAggregateProxy" transports="https http" startOnLoad="true" trace="disable">
    <target>
        <inSequence>
            <property name="TRANSPORT_HEADERS" scope="axis2" action="remove"/>
            <log level="full">
                <property xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:weat="http://ws.cdyne.com/WeatherWS/" name="uri.var.zip" expression="/soapenv:Envelope/soapenv:Body/weat:GetCityWeatherByZIP/weat:ZIP"/>
            </log>
            <filter xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:weat="http://ws.cdyne.com/WeatherWS/" xpath="/soapenv:Envelope/soapenv:Body/weat:GetCityWeatherByZIP">
                <then>
                    <property name="uri.var.zip" expression="/soapenv:Envelope/soapenv:Body/weat:GetCityWeatherByZIP/weat:ZIP" scope="default" type="STRING" description=""/>
                    <log level="full">
                        <property name="zipCode" expression="get-property('uri.var.zip')"/>
                    </log>
                    <clone id="edbIhub">
                        <target>
                            <endpoint>
                                <address uri="http://wsf.cdyne.com/WeatherWS/Weather.asmx"/>
                            </endpoint>
                        </target>
                        <target>
                            <endpoint>
                                <http method="get" uri-template="http://api.openweathermap.org/data/2.5/weather?q={uri.var.zip}"/>
                            </endpoint>
                        </target>
                    </clone>
                </then>
                <else/>
            </filter>
        </inSequence>
        <outSequence>
            <property name="messageType" value="application/xml" scope="axis2" type="STRING" description="messageType"/>
            <log level="full" description="">
                <property name="WSANSWER" value="true"/>
            </log>
            <aggregate>
                <completeCondition timeout="60">
                    <messageCount min="2" max="2"/>
                </completeCondition>
                <onComplete xmlns:s12="http://www.w3.org/2003/05/soap-envelope" xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/" expression="/s11:Envelope/s11:Body/child::*[position()=1] | /s12:Envelope/s12:Body/child::*[position()=1]">
                    <log level="full" description="json">
                        <property name="json" expression="$body/jsonObject"/>
                    </log>

                    <property name="messageType" value="application/xml" scope="axis2"/>
                </onComplete>
            </aggregate>
            <send/>
        </outSequence>
        <faultSequence/>
    </target>
</proxy>

在xml中休息响应(使用jsonObject)。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><coord><lon>-96.99</lon><lat>32.87</lat></coord><sys><message>0.0237</message><country>US</country><sunrise>1428408443</sunrise><s
352</sunset></sys><weather><id>800</id><main>Clear</main><description>Sky is Clear</description><icon>01d</icon></weather><base>stations</base><main><temp>301.125</temp><temp_min>301.125</temp_min>
<temp_max>301.125</temp_max><pressure>10e><sea_level>1027.22</sea_level><grnd_level>1003.5</grnd_level>
<humidity>69</humidity></main><wind><speed>8.07</speed><deg>200.504</deg></wind><clouds><all>0</all></clouds>
<dt>1428433031</dt><id>0</id><name>Irving</name><cod>200</cod></j
oapenv:Body></soapenv:Envelope>

SOAP响应。

<?xml version="1.0"
-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.
/"><GetCityWeatherByZIPResult><Success>true</Success><ResponseText>City Found</ResponseText><State>TX</State><City>Irving</City><WeatherStationCity>Euless</WeatherStationCity>
<WeatherID>14</WeatherID><Description>Cloudy</Description><TemTemperature>
<RelativeHumidity>84</RelativeHumidity><Wind>NE7</Wind><Pressure>29.86S</Pressure><Visibility></Visibility>
<WindChill></WindChill><Remarks></Remarks></GetCityWeatherByZIPResult></GetCityWeatherByZIPResponse></soap:Body>
</soap>

1 个答案:

答案 0 :(得分:-1)

您应该能够使用payloadFactoryMediator [1]。为了这。在OnSemplete内部OnComplete中使用有效负载Factory mediator来构建您需要的响应

E.g。

<payloadFactory media-type="xml">
    <format>
         <m:GetCityWeatherByZIPResponse xmlns:m="http://ws.cdyne.">
              <m:GetCityWeatherByZIPResult>
                  <m:Success>true</m:Success>
                  .....
                  .....

              </m:GetCityWeatherByZIPResult>
         </m:GetCityWeatherByZIPResponse>
    </format>
    <args>
        ...
        ...
   </args>
</payloadFactory>

[1] - https://docs.wso2.com/display/ESB481/PayloadFactory+Mediator