WSO2 API PayloadFactory mediator XML array to JSON array

北城余情 提交于 2020-01-03 03:29:10

问题


I am trying to build a Proxy between a client and a server using WSO2-AM. The ultimate goal is changing the content of the response the server sends to a format that the client understands. Therefore I wrote a custom mediator using a PayloadFactory. This works OK when the server sends just 1 element, but it can be possible that the servers sends multiple elements. I am trying to use an iterator to iterate all the elements and aplying a payloadfactory to each element, but this only returns the first element, or (when the response of the server is enclosed with a root element) the last element.

The server output looks something like this:

<Monitoring>
    <Line>
        <Id>1</Id>
    </Line>
    <LocationPoint>
        <Direction>
            <Id>11</Id>

        </Direction>
        <DistanceFromPoint>1111</DistanceFromPoint>
        <Point>
            <Id>11111</Id>
        </Point>
    </LocationPoint>
</Monitoring>
<!-- Repeated 0 ... x times -->

I apply my mediotor which looks like this:

<sequence xmlns="http://ws.apache.org/ns/synapse" name="NAME">
    <iterate
         preservePayload="true"
         expression="//Monitoring"
         id="MONITORING_ITERATOR">  
         <target>
            <sequence>
            <payloadFactory media-type="json">
                <format>
                    {
                        "LineId":"$1",
                        "DirectionId":"$2",
                        "DistanceFromPoint":"$3",
                        "PointId":"$4"
                    }
                </format>
                <args>
                    <arg expression="//Line/Id"/>
                    <arg evaluator="xml" expression="//Direction/Id"/>
                    <arg evaluator="xml" expression="//DistanceFromPoint"/>
                    <arg evaluator="xml" expression="//Point/Id"/>
                </args>
            </payloadFactory>
            <send />
            </sequence>
            </target>
    </iterate>
    <property name="messageType" expression="get-property('default', 'WSO2_AM_API_ACCEPT_MESSAGE_TYPE')" scope="axis2"/>
</sequence>

So my expected output would be an array of the json objects declared between the format tags. However only 1 element is outputed. What am I missing?

EDIT: I also want to dynamicly get a json or xml response based on the ACCEPT header. Normally this is possible by saving the header at the IN flow and then use

<property name="messageType" expression="get-property('default', 'WSO2_AM_API_ACCEPT_MESSAGE_TYPE')" scope="axis2"/>

But this doesn't seem to work here either.


回答1:


Since you are directly mapping the xml element to a json element (not changing the structure), you can simply change the content-type of the message in the sequence and api manager (esb in api manager) will automatically convert the xml to json message for your. You won't have to use the iterate or payload factory mediator

<sequence xmlns="http://ws.apache.org/ns/synapse"  name="xml_to_json_out_message">      
    <property name="messageType" value="application/json" scope="axis2"/>
</sequence>

This will convert the xml to json




回答2:


After intensive searching for an answer, I didn't find any examples that related to my problem. I now use a class mediator written in Java. Tis is not only more flexible, but also easier to debug.



来源:https://stackoverflow.com/questions/35502509/wso2-api-payloadfactory-mediator-xml-array-to-json-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!