我不能让Mule ESB在'条件
时触发选择'我将Hello World流转换为接受aJSON对象(将JSON插入到Object节点)并执行Choice节点。
但是我不能触发CHoice声明的'when'条件;我总是转到DEFAULT案例。以下是流程:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:json="http://www.mulesoft.org/schema/mule/json" version="EE-3.5.0" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<json:object-to-json-transformer name="Object_to_JSON" doc:name="Object to JSON"/>
<flow doc:name="HelloWorldFlow1" name="HelloWorldFlow1">
<http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081" contentType="application/json"/>
<json:json-to-object-transformer doc:name="JSON to Object" />
<choice doc:name="Choice-Determine-Route" tracking:enable-default-events="true">
<when expression="#[payload.uid == "\042ABC\042"]">
<set-payload value="#['When ABC case']" doc:name="Set Payload"/>
</when>
<otherwise>
<set-payload value="#['Default case, payload: ' + payload + ', payload.get(\047uid\047): ' + payload.get('uid')]" doc:name="Set Payload"/>
</otherwise>
</choice>
</flow>
</mule>
在这个流程中,我使用以下条件和SetPayload(默认情况)语句:
#[payload.uid == "\042ABC\042"]
( Note: I used the \042 (double quote) because JSONPATH expressions will return "ABC" instead of ABC )
#['Default case, payload: ' + payload + ', payload.get(\047uid\047): ' + payload.get('uid')]
以下是我的输出:
C:\curl>type input2.txt
{ "uid" : "ABC" }
C:\curl>curl -H "Content-Type: application/json" -i -d @input2.txt http://localh
ost:8081
Default case, payload: {"uid":"ABC"}, payload.get('uid'): "ABC"
如您所见,我没有触发'when'子句。但是,SetPayload确实将payload.get('uid')输出为“ABC”
所以我再尝试三次,有三种不同的“何时”条件:
#[payload.uid == "ABC"]
#[payload.get('uid') == "ABC"]
#[payload.get('uid') == "\042ABC\042"]
但在所有这三种情况中,我得到以下内容:
Default case, payload: {"uid":"ABC"}, payload.get('uid'): "ABC"
我该如何解决这个问题?
由于
答案 0 :(得分:2)
试试这个: -
<强>更新: - 强>
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
<choice doc:name="Choice-Determine-Route" tracking:enable-default-events="true">
<when expression="#[message.payload.uid == 'ABC']">
<set-payload value="When ABC case" doc:name="Set Payload"/>
</when>
<otherwise>
<set-payload value="#['Default case, payload: ' + payload + ', payload.get(\047uid\047): ' + payload.get('uid')]" doc:name="Set Payload"/>
</otherwise>
</choice>
答案 1 :(得分:1)
在选择
之前,为JSON转换器尝试以下操作<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
在choice-when表达式中使用以下
<when expression="#[payload.get('UID') == 'ABC']" >
希望这有帮助。