在 mule 中创建一个数组列表并对其进行迭代

Create an arraylist in mule and iterate over it

我想创建一个 arrayList 并遍历其条目以检查我的消息是否具有设置的属性

        <set-variable variableName="ex" value="#[{'A', 'User-Agent', 'Application-ID', 'API-Key', 'P', 'Organization-ID'}]" doc:name="Set Variable" />
        <for-each collection="#[ex]" doc:name="Foreach">
           <choice doc:name="Choice">
             <when expression="#[message.inboundProperties.contains([payload]) == false]">
                <set-variable variableName="isRequestValid" value="false" doc:name="Variable"/>
             </when>
           </choice>
        </for-each>

我在 for-each 表达式中遇到错误(发现以元素 'for-each' 开头的内容无效)。此外,我们可以在没有其他选项的情况下使用选择块吗?

您可能使用的是旧版本的 Mule,for-each 结构包含在 3.4 中(参见 release notes),我想不出导致该错误的其他原因。 不幸的是,您不能在没有其他情况的情况下使用 choice,但是报告有一个改进:https://www.mulesoft.org/jira/browse/MULE-6129

应该是foreach而不是for-each

我们可以在没有否则选项的情况下使用选择块。但是当值与when选项中的逻辑不匹配时会引入运行时错误。

由于您已经通过 for-each 解决了问题,剩余的问题可以使用以下表达式解决

#[message.inboundProperties[payload] !=null]

如果 returns true 则 header 否则不存在。

希望对您有所帮助。

  1. 应该是foreach
  2. 列表可以初始化为#[['A','B']]
  3. 如果你不需要其他方式,即只是过滤掉有效负载,你可以在 foreach -
  4. 中使用 expression-filter
    <set-variable variableName="ex" value="#[['A', 'User-Agent', 'Application-ID', 'API-Key', 'P', 'Organization-ID']]" doc:name="Set Variable" />
    <foreach collection="#[ex]" doc:name="Foreach">
        <expression-filter expression="#[message.inboundProperties.contains([payload]) == false]" doc:name="Expression"/>
        <set-variable variableName="isRequestValid" value="false" doc:name="Variable"/>
    </foreach>