在 wso2 esb 中使用脚本中介删除重复的数组元素

Remove duplicates array elemets using Script mediator in wso2 esb

我想使用 wso2 esb 脚本中介程序删除重复项。我正在使用 WSO2 ESB 为需要 return 转换后的 JSON 数组的指定端点创建代理服务。我已经在输出序列中设置了一个带有 foreach 调解器和 payloadfactory 的代理服务。结果是 Failed to Publish Environments 生产和 Sandbox:Error 在网关更新序列。结果是只有最后一个元素是 returned.

我有一个 json 对象和重复元素数组

原始端点return是这样的:

{
  "colors": [
    "Red",
    "Blue",
    "Red",
    "Yellow",
    "White",
    "Yellow"
  ]
}

所需结果:

{
  "colors": [
    "Blue",
    "Red",
    "Yellow",
    "White"]
}

我的调解政策:

<sequence
    xmlns="http://ws.apache.org/ns/synapse"  name="legalCost">
    <!-- Used to extract the elements from the JSON payload and then assigns them to the variables  and  respectively. First argument is assigned to the variable  and the second to -->
    <payloadFactory media-type="json">
        <!-- Here you specify the format of the json message you need to output -->
        <format>
                            {
                              "products": 
                            }
                        </format>
        <foreach expression="///jsonArray/jsonElement"" name="JSONPayload" evaluator="json">
            <!-- REturns all <foreach expression="$.policyItemSummary.[?(@.productLine)]" evaluator="json"/> -->
            <script language="js">
                <![CDATA[
                    var payload = mc.getProperty("JSONPayload");
                    results = payload.results;
                    response = new Array();
                    for (i = 0; i &lt; results.length; ++i) {
                        if (response.indexOf(payload[i]) === -1) {
                            response.push(payload[i]);
                        }
                       }
                    mc.setPayloadJSON(response);
                    ]]>
            </script>
        </foreach>
        <args>
            <arg evaluator="json" expression="get-property('response')"/>
        </args
    
    </payloadFactory>
    <property name="messageType" value="application/json" scope="axis2"/>
</sequence> 

让我们稍微修改一下提到的调解顺序来满足您的要求。下面给出的是中介序列的摘录,用于读取响应有效负载并在过滤后将其设置回去

<property name="JSONPayload" expression="json-eval($.)" />
<script language="js">
    <![CDATA[
        importPackage(Packages.java.util);
        var log = mc.getServiceLog();
        
        var response = mc.getProperty("JSONPayload");
        var jsonPayload = JSON.parse(response);
        var colorList = jsonPayload['colors'];

        filtered = new Array();
        for (i = 0; i < colorList.length; ++i) {
            if (filtered.indexOf(colorList[i]) === -1) {
                filtered.push(colorList[i]);
            }
        }

        // printing the elements
        // for (var i = 0; i < filtered.length; i++) {
        //    log.info(filtered[i]);
        // }

        jsonPayload['colors'] = filtered;
        // log.info(JSON.stringify(jsonPayload));

        mc.setPayloadJSON(jsonPayload);
    ]]>
</script>
<!-- continue on setting up the response in the payload factory -->

希望这可以帮助您实现您的要求