Mule:在ArrayList上迭代
我试图将ArrayList保存到flowVar中&然后在foreach语句中迭代flowVar,代码如下: 有效负载[2]是一个数组,它是arrayList的一部分Mule:在ArrayList上迭代,mule,Mule,我试图将ArrayList保存到flowVar中&然后在foreach语句中迭代flowVar,代码如下: 有效负载[2]是一个数组,它是arrayList的一部分 <foreach collection="#[flowVars['ID']]" doc:name="For Each"> .... </foreach> .... 但是,当我尝试运行此代码时,出现以下错误: org.mule.exception.defa
<foreach collection="#[flowVars['ID']]" doc:name="For Each">
....
</foreach>
....
但是,当我尝试运行此代码时,出现以下错误:
org.mule.exception.defaultMessaginExceptionStrategy:
消息:不是语句(org.mule.mvel2.ScriptRuntimeException)。消息负载的类型为:LinkedList
有什么想法吗?您可以像下面的示例一样轻松地实现它,方法是将变量声明为
java.util.ArrayList
,如下所示:-
<set-variable variableName="ID" value="#[new java.util.ArrayList(Arrays.asList('abc','def',66))]" doc:name="Variable"/>
<foreach collection="#[flowVars.ID]" counterVariableName="i" doc:name="For Each">
<logger message="Value:- #[message.payload]" level="INFO" doc:name="Logger"/>
</foreach>
注意:如果您想获取Arraylist的特定索引,您不需要foreach,您可以这样做:-
<logger message="value at index 2: #[flowVars.ID[2]]" level="INFO" doc:name="Logger"/>
在表达式组件中交替地:
<set-variable variableName="ID" value="#[new java.util.ArrayList(Arrays.asList('abc','def',66))]" doc:name="Variable"/>
<expression-component doc:name="Expression"><![CDATA[
for(int i=0;i<flowVars.ID.size();i++)
{
System.out.println("Value "+ flowVars.ID[i]);
}
]]></expression-component>
通过将变量声明为
java.util.ArrayList
,您可以像下面的示例一样轻松地执行
<set-variable variableName="ID" value="#[new java.util.ArrayList(Arrays.asList('abc','def',66))]" doc:name="Variable"/>
<foreach collection="#[flowVars.ID]" counterVariableName="i" doc:name="For Each">
<logger message="Value:- #[message.payload]" level="INFO" doc:name="Logger"/>
</foreach>
注意:如果您想获取Arraylist的特定索引,您不需要foreach,您可以这样做:-
<logger message="value at index 2: #[flowVars.ID[2]]" level="INFO" doc:name="Logger"/>
在表达式组件中交替地:
<set-variable variableName="ID" value="#[new java.util.ArrayList(Arrays.asList('abc','def',66))]" doc:name="Variable"/>
<expression-component doc:name="Expression"><![CDATA[
for(int i=0;i<flowVars.ID.size();i++)
{
System.out.println("Value "+ flowVars.ID[i]);
}
]]></expression-component>
@insaneyogi,下面是一个以flowVar形式在arrayList上迭代的流示例:
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="test-foreachFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/foreach" doc:name="HTTP"/>
<expression-component doc:name="Expression"><![CDATA[myArray = new java.util.ArrayList();
myArray.add("el1");
myArray.add("el2");
myArray.add("el3");
flowVars.myArray = myArray;]]></expression-component>
<foreach collection="#[flowVars.myArray]" doc:name="For Each">
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</foreach>
</flow>
</mule>
@insaneyogi,下面是一个以flowVar形式在arrayList上迭代的流示例:
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="test-foreachFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/foreach" doc:name="HTTP"/>
<expression-component doc:name="Expression"><![CDATA[myArray = new java.util.ArrayList();
myArray.add("el1");
myArray.add("el2");
myArray.add("el3");
flowVars.myArray = myArray;]]></expression-component>
<foreach collection="#[flowVars.myArray]" doc:name="For Each">
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</foreach>
</flow>
</mule>
快速提问Anirban,如果我正在处理一组数组,那么[flowVars.ID[I][2]]应该正常工作吗?我有一个数组,我需要检查数组中的每一个第二个元素是否都存在于DB中,或者Anirban的notQuick问题,如果我正在处理一个数组,那么#[flowVars.ID[I][2]]应该正常工作吗?我有一个数组,需要检查数组中的第二个元素是否存在于DB中