WSO2 API PayloadFactory 中介器 XML 数组到 JSON 数组 [英] WSO2 API PayloadFactory mediator XML array to JSON array

查看:26
本文介绍了WSO2 API PayloadFactory 中介器 XML 数组到 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WSO2-AM 在客户端和服务器之间构建代理.最终目标是将服务器发送的响应内容更改为客户端可以理解的格式.因此,我使用 PayloadFactory 编写了一个自定义中介.当服务器仅发送 1 个元素时,这可以正常工作,但服务器可能发送多个元素.我正在尝试使用迭代器来迭代所有元素并将有效载荷工厂应用于每个元素,但这仅返回第一个元素,或(当服务器的响应包含根元素时)最后一个元素.

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.

服务器输出如下所示:

<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>

所以我的预期输出将是格式标记之间声明的 json 对象数组.但是只输出 1 个元素.我错过了什么?

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?

我还想根据 ACCEPT 标头动态获取 json 或 xml 响应.通常这可以通过在 IN 流中保存标头然后使用

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.

推荐答案

在深入寻找答案后,我没有找到任何与我的问题相关的示例.我现在使用用 Java 编写的类中介器.这不仅更灵活,而且更容易调试.

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.

这篇关于WSO2 API PayloadFactory 中介器 XML 数组到 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆