Mule ESB:无法通过“Choice”节点路由传入的JSON对象

时间:2014-07-15 17:56:46

标签: json mule esb flow choice

(Mods:我检查了与我类似的其他线程,但这个问题处于更深层次)

我将Hello World流转换为接受JSON对象的流,并且我尝试使用'Choice'流控制节点将其路由到两个'Set Payload'节点之一。

我的问题是我可以让流选择节点路由到正确的目标节点

(我怀疑这是一个JSON问题)

以下是我的流程

<?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"/>
        <choice doc:name="Choice-Determine-Route" tracking:enable-default-events="true">
            <when expression="#[payload.uid == 'ABC']">
                <set-payload value="#['When ABC case']" doc:name="Set Payload"/>
            </when>
            <otherwise>
                <set-payload value="#['Default case, uid: '  + payload.uid + ',  payload:  ' + payload]" doc:name="Set Payload"/>
            </otherwise>
        </choice>
    </flow>
</mule>

我正在向流发送以下JSON字符串

{ "uid" : "ABC" }

因此流程应该能够检测到uid等于'ABC'

以下是我的测试程序

C:\curl>curl -H "Content-Type: application/json" -i -d @input2.txt http://localh
ost:8081
HTTP/1.1 200 OK
Date: Tue, 15 Jul 2014 09:42:34 -0700
Server: Mule EE Core Extensions/3.5.0
Content-Type: application/json
X-MULE_SESSION: rO0ABXNyACNvcmcubXVsZS5zZXNzaW9uLkRlZmF1bHRNdWxlU2Vzc2lvbi7rdtEW
7GGKAwAFWgAFdmFsaWRMAA1mbG93Q29uc3RydWN0dAAmTG9yZy9tdWxlL2FwaS9jb25zdHJ1Y3QvRmxv
d0NvbnN0cnVjdDtMAAJpZHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wACnByb3BlcnRpZXN0AA9MamF2YS91
dGlsL01hcDtMAA9zZWN1cml0eUNvbnRleHR0ACdMb3JnL211bGUvYXBpL3NlY3VyaXR5L1NlY3VyaXR5
Q29udGV4dDt4cAFwdAAkMDU2OTdkYjEtMGMzZi0xMWU0LWExYjEtMDAyMTVlNmIzZDkwc3IAJWphdmEu
dXRpbC5Db2xsZWN0aW9ucyRTeW5jaHJvbml6ZWRNYXAbc/kJS0s5ewMAAkwAAW1xAH4AA0wABW11dGV4
dAASTGphdmEvbGFuZy9PYmplY3Q7eHBzcgAkb3JnLm11bGUudXRpbC5DYXNlSW5zZW5zaXRpdmVIYXNo
TWFwndHZ72dFzgADAAB4cHcMP0AAAAAAABAAAAAAeHEAfgAJeHB4
X-MULE_ENCODING: UTF-8
Content-Length: 99
Connection: close

Default case, uid: null,  payload:  org.apache.commons.httpclient.ContentLengthI
nputStream@42884288

正如你所看到的,我发送了一个'ABC'的uid,但它仍然是默认情况

正如您在流程中看到的那样,我在“设置有效负载”节点中打印出相关的有效负载信息:

#['默认情况,uid:'+ payload.uid +',有效负载:'+有效负载]

...正如您从测试程序中看到的那样,我从该语句中得到以下输出:

Default case, uid: null,  payload:  org.apache.commons.httpclient.ContentLengthI
nputStream@42884288

2个问题

  • 如何解决此问题,以便我可以点击'ABC'路线?

  • 究竟是什么“org.apache.commons.httpclient.ContentLengthInputStream@42884288

    我知道它是一个输入流,但数据在哪里?

由于

2 个答案:

答案 0 :(得分:2)

您可以使用<object-to-string-transformer />转换流。获取json的字符串表示形式。您还可以使用其他变换器,例如object-to-byte-array

然后,您需要首先将json转换为对象,以使选择表达式起作用。

<json:json-to-object-transformer returnClass="java.util.HashMap" />

答案 1 :(得分:0)

或者您可以使用: -

<json:json-to-object-transformer returnClass="java.lang.Object" />

然后#[message.payload.uid]获取值。
请注意,使用#[message.payload.uid]#[payload.uid]更好,被认为是更好的做法。