使用MEL在Mule Flow中将Map设置为有效载荷

use*_*140 3 mule mule-el

我正在尝试生成并设置一个映射(具有2个键值对)作为后续HTTP调用的有效负载.但是,用于创建Map的MEL表达式不起作用.

<sub-flow name="call-myservice" doc:name="call-myservice">
    <set-payload value="#[username :${my.username}, password : ${my.password}]" doc:name="Set Payload"/>
    <https:outbound-endpoint exchange-pattern="request-response" host="${myservice.Host}"  method="POST" mimeType="application/json" doc:name="My Service call" path="mypath" port="443"/>
</sub-flow>
Run Code Online (Sandbox Code Playgroud)

我按照http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+MEL上的说明进行操作

这暗示 -

MEL provides a streamlined way to access map data. 

Rather than constructing a map with a new statement, and then using its put method to populate it, you can simply write the following:

[key1 : value1, key2 : value2, . . .]
Run Code Online (Sandbox Code Playgroud)

但是,它给了我以下例外 -

ERROR 2014-02-28 15:27:51,424 [[services-proxy].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Execution of the expression "username :abc, password : pwd" failed.         (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. [Error: unresolvable property or identifier: username]
[Near : {... username :abc, pa ....}]
         ^
[Line: 1, Column: 1] (org.mvel2.PropertyAccessException)
  org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer:687 (null)
Run Code Online (Sandbox Code Playgroud)

Seb*_*eba 7

您缺少方括号来分隔地图(您只能分隔Mule表达式).将其更改为:

<set-payload value="#[['username' :${my.username}, 'password' : ${my.password}]]" doc:name="Set Payload"/>
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用属性占位符,我认为您还需要引用值:<set-payload value ="#[['username':'$ {my.username}','password':'$ {my.password}' ]]"doc:name ="设置有效负载"/>. (3认同)