使用wso2 esb api聚合响应:没有答案

时间:2014-10-31 14:52:12

标签: json rest wso2 esb

在WSO2 ESB中,我创建了一个rest API。此API需要连接到两个不同的服务,并将结果组合在一起。

为此,我使用一个API,在de insequence中发送一个克隆中介,发送相应的消息。这很好。

在结果中,我使用记录器来查看传入的消息。两个都是json并且回来了。然后我想使用聚合调解器来组合这两者。有了这个,我有两个问题:

  • 当使用rootElementProperty将两个项目放在一个父项下时,会发生NPE,
  • 当使用富集调解器或其他任何东西来制作正确的消息,并以发送结束时,结果消息永远不会被发回。

这可能是什么原因?

这是实际的API:

<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="Cale-StreetsAPI" context="/Cale-StreetsAPI">
    <resource methods="GET" uri-template="/getStreets?lat={lat}&amp;lon={lon}&amp;radius={radius}">
        <inSequence>
            <property name="aggRoot" scope="default">
                <red:jsonObject xmlns:red="redora"/>
            </property>
            <log description="">
                <property name="lat" expression="get-property('uri.var.lat')"/>
                <property name="lon" expression="get-property('uri.var.lon')"/>
                <property name="radius" expression="get-property('uri.var.radius')"/>
            </log>
            <clone continueParent="true" id="deCartaStreets">
                <target>
                    <sequence>
                        <sequence key="DeCartaDDS"/>
                    </sequence>
                </target>
                <target>
                    <sequence>
                        <sequence key="DeCartaReverseGeo"/>
                    </sequence>
                </target>
            </clone>
        </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>
                    <messageCount min="2" max="-1"/>
                </completeCondition>
                <onComplete expression="$body/jsonObject">
                    <enrich>
                        <source clone="true" xpath="$body//jsonObject"/>
                        <target type="property" property="ResultItems"/>
                    </enrich>
                    <log level="custom">
                        <property name="ResultItems" expression="get-property('ResultItems')"/>
                    </log>
                    <enrich>
                        <source type="inline" clone="true">
                            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                                <soapenv:Header/>
                                <soapenv:Body>
                                    <JsonResponse xmlns="dummy"/>
                                </soapenv:Body>
                            </soapenv:Envelope>
                        </source>
                        <target type="envelope"/>
                    </enrich>
                    <enrich>
                        <source type="property" clone="true" property="ResultItems"/>
                        <target xmlns:red="dummy" action="child" xpath="//red:JsonResponse"/>
                    </enrich>
                    <log level="full" description="">
                        <property name="AGGREGATE_END" value="true"/>
                    </log>
                    <property name="messageType" value="application/json" scope="axis2" type="STRING" description="messageType"/>
                    <send/>
                </onComplete>
            </aggregate>
        </outSequence>
        <faultSequence>
            <log level="full" category="WARN"/>
        </faultSequence>
    </resource>
</api>

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。根据Internet上的帖子数量API不支持聚合。

所以我的解决方案是引入聚合代理服务,它完成了API只调用该代理服务的所有工作。

因此,如果按照以下方式修改解决方案,您应该能够实现预期目标:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"  name="AggregatingProxy" transports="local https http"  startOnLoad="true" trace="disable">
<target>
    <inSequence>
        <log level="full"/>
        <property name="enclosing_element">
            <result xmlns=""/>
        </property>
        <clone continueParent="true">
            <property name="aggRoot" scope="default">
                <red:jsonObject xmlns:red="redora"/>
            </property>
            <log description="">
                <property name="lat" expression="get-property('uri.var.lat')"/>
                <property name="lon" expression="get-property('uri.var.lon')"/>
                <property name="radius" expression="get-property('uri.var.radius')"/>
            </log>
            <clone continueParent="true" id="deCartaStreets">
                <target>
                    <sequence>
                        <sequence key="DeCartaDDS"/>
                    </sequence>
                </target>
                <target>
                    <sequence>
                        <sequence key="DeCartaReverseGeo"/>
                    </sequence>
                </target>
            </clone>
        </clone>
    </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>
                <messageCount min="2" max="-1"/>
            </completeCondition>
            <onComplete expression="$body/jsonObject">
                <enrich>
                    <source clone="true" xpath="$body//jsonObject"/>
                    <target type="property" property="ResultItems"/>
                </enrich>
                <log level="custom">
                    <property name="ResultItems" expression="get-property('ResultItems')"/>
                </log>
                <enrich>
                    <source type="inline" clone="true">
                        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                            <soapenv:Header/>
                            <soapenv:Body>
                                <JsonResponse xmlns="dummy"/>
                            </soapenv:Body>
                        </soapenv:Envelope>
                    </source>
                    <target type="envelope"/>
                </enrich>
                <enrich>
                    <source type="property" clone="true" property="ResultItems"/>
                    <target xmlns:red="dummy" action="child" xpath="//red:JsonResponse"/>
                </enrich>
                <log level="full" description="">
                    <property name="AGGREGATE_END" value="true"/>
                </log>
                <property name="messageType" value="application/json" scope="axis2" type="STRING" description="messageType"/>
                <send/>
            </onComplete>
        </aggregate>
    </outSequence>
    <faultSequence>
        <log level="full" category="WARN"/>
    </faultSequence>
</target>
</proxy>

API配置:

<api xmlns="http://ws.apache.org/ns/synapse" name="Cale-StreetsAPI" context="/Cale-StreetsAPI">
<resource methods="GET" uri-template="/getStreets?lat={lat}&amp;lon={lon}&amp;radius={radius}">
    <inSequence>
        <call>
            <endpoint>
                <address uri="http://localhost:8280/services/AggregatingProxy" format="soap12"></address>
            </endpoint>
        </call>
        <respond/>
    </inSequence>
</resource>
</api>

这实际上对我有用,希望对你也有所帮助。

还考虑使用:

<property name="enclosing_element">
            <result xmlns=""/>
        </property>
   ...
   <onComplete expression="$body/jsonObject" enclosingElementProperty="enclosing_element">

如果您的服务返回不同类型的结果。

谢谢, 弗拉基米尔。