使用foreach mediator

时间:2017-01-10 16:42:34

标签: wso2 wso2esb

我正在使用WSO2 ESB V4.9.0为指定的端点创建代理服务,该端点需要返回已转换的JSON数组。 我已经在out序列中设置了一个foreach mediator和payloadfactory的代理服务以及日志语句,以查看发生了什么。 结果是每个单独的数组元素都按预期转换,但是单个元素在循环结束时不会合并回单个数组 正如文件似乎暗示的那样。结果是只返回最后一个元素。

我唯一能看到的是,我见过的大多数例子都不只是一个带有数组的元素而不仅仅是一个数组。有人知道这是否可行?

原始端点返回如下内容:

[
{"id": "1",
"type": "object",
"name": "first",
"bounds":"[[-0.256,51.531],[-0.102,51.656]]",
"displayName": "first record"},

{"id": "2",
"type": "object",
"name": "second",
"bounds":"[[-0.256,51.531],[-0.102,51.656]]",
"displayName": "second record"},
..etc...
]

代理服务所需的回报是这样的:

[
{"name": "first record",
"bounds":"[[-0.256,51.531],[-0.102,51.656]]"},
{"name": "second record",
"bounds":"[[-0.256,51.531],[-0.102,51.656]]"},
..etc...
]

代理的实际回报如下:

{"name": "first record",
"bounds":"[[-0.256,51.531],[-0.102,51.656]]"}

代理的配置在这里:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="newfeed_v2"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log/>
      </inSequence>
      <outSequence>
         <log level="full">
            <property name="MESSAGE" value="FIRST"/>
         </log>
         <foreach id="foreach_1" expression="//jsonArray/jsonElement">
            <sequence>
               <payloadFactory media-type="json">
                  <format>{ 
   "name" : "$1", 
   "bounds" : "$2"  
}</format>
                  <args>
                     <arg evaluator="json" expression="$.displayName"/>
                     <arg evaluator="json" expression="$.bounds"/>
                  </args>
               </payloadFactory>
                <log level="full">
                  <property name="MESSAGE" value="ENDLOOP"/>
               </log>
            </sequence>
         </foreach>
         <log level="full">
            <property name="MESSAGE" value="LAST"/>
         </log>
         <log/>
         <send/>
      </outSequence>
      <endpoint>
         <address uri="https://api.backend.com"/>
      </endpoint>
   </target>
   <description/>
</proxy>

2 个答案:

答案 0 :(得分:4)

您可以尝试将其作为解决方法:

{"value":[{"name":"first record","bounds":"[[-0.256,51.531],[-0.102,51.656]]"},{"name":"second record","bounds":"[[-0.256,51.531],[-0.102,51.656]]"},{"name":"tercer record","bounds":"[[-0.256,51.531],[-0.102,51.656]]"}]}

我的回答:

section {
  display: none;
}
#about:target,
#services:target,
#contact:target {
  display: block;
}

答案 1 :(得分:0)

每个介体的

都会将splitted表达式添加到根元素中,所以我这样做了,

{ "result": [{"name": "first record","bounds": [[-0.256,51.531],[-0.102,51.656]]},{"name": "second record","bounds": [[-0.256,51.531],[-0.102,51.656]]},{"name": "tercer record","bounds": [[-0.256,51.531],[-0.102,51.656]]}]}

我得到了这样的回复

String s="(decode(W_Employee_D_3.Fst_Name,NULL,"
        + "decode(W_Employee_D_3.Last_Name,NULL,"
        + "decode(W_Employee_D_3.Mid_Name,NULL,'emptyString','midnamevailable'),'lastnameavil'),"
        + "concat(CONCAT ( concat(W_Employee_D_3.last_Name, ' ,'),"
        + "W_Employee_D_3.Fst_Name ),W_Employee_D_3.Mid_Name)))";