Mule ESB:无法取消引用JSON对象数组

时间:2014-07-18 21:23:49

标签: json mule esb jsonpath

我有一个返回以下JSON对象的流:

[{"1":444}]

“1”表示select sum()语句中的数据库行。

这是一个不寻常的JSON对象,在JSONPath中取消引用444值的不寻常方法如下:

$.[0].1 

这是我用来打印整个JSON对象的流程:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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" version="EE-3.5.0"
    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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <db:generic-config name="Generic_Database_Configuration" url="jdbc:db2://localhost:50000/TEST:user=instuid;password=instpw;" driverClassName="com.ibm.db2.jcc.DB2Driver" doc:name="Generic Database Configuration"/>
    <flow name="test2Flow1" doc:name="test2Flow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <db:select config-ref="Generic_Database_Configuration" doc:name="Database" doc:description="test">
            <db:parameterized-query><![CDATA[SELECT SUM(BALANCE) FROM BENCHMARKING.ACCOUNT]]></db:parameterized-query>
        </db:select>
        <response>
            <set-payload value="#['---&gt;  , payload: ' + payload]" doc:name="Set Payload"/>
        </response>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>
</mule>

所以我使用下面的SetPayload表达式,得到以下结果:

#['--->  , payload: ' + payload]


C:\curl>curl http://localhost:8081
--->  , payload: [{"1":444}]
C:\curl>

所以现在我想简单地提取值444.所以我想我需要做一些像

的JSONPath语句
$.[0].1 

所以我尝试以下SetPayload表达式并得到以下结果:

#['--->  , payload: ' + payload.[0].1]


C:\curl>curl http://localhost:8081
[Error: unbalanced braces { ... }]
[Near : {... '--->  , payload: ' + payload.[0].1 ....}]
                                            ^
[Line: 1, Column: 32] (org.mule.api.expression.InvalidExpressionException). Mess
age payload is of type: String

好的,这是有道理的,所以我再试一次:

#['--->  , payload: ' + payload.\1330\135.1]

C:\curl>curl http://localhost:8081
[Error: unexpected end of statement]
[Near : {... '--->  , payload: ' + payload.\1330\135.1 ....}]
                                   ^
[Line: 1, Column: 23] (org.mule.api.expression.InvalidExpressionException). Mess
age payload is of type: String

那么如何编写这个SetPayload表达式,以便在给定

的JSON对象的情况下返回值444
[{"1":444}]

由于

修改

我改变了我的流程如下:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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" version="EE-3.5.0"
    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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <db:generic-config name="Generic_Database_Configuration" url="jdbc:db2://localhost:50000/TEST:user=instuid;password=instpw;" driverClassName="com.ibm.db2.jcc.DB2Driver" doc:name="Generic Database Configuration"/>
    <flow name="test2Flow1" doc:name="test2Flow1">
            <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" path="mule/getBalance"/>
        <db:select config-ref="Generic_Database_Configuration" doc:name="FB Database" doc:description="test">
            <db:parameterized-query><![CDATA[SELECT SUM(BALANCE) FROM BENCHMARKING.ACCOUNT]]></db:parameterized-query>
        </db:select>
        <set-payload value="#[message.payload[0]['1']]" doc:name="Set Payload"/>
        <json:json-to-object-transformer doc:name="JSON to Object"/>
    </flow>
</mule>

但是,我得到以下输出:

C:\curl>curl http://localhost:8081/mule/getBalance
¼φ ♣sr ↔org.mule.module.json.JsonDataZ╫♦♠╓▀☺┤☻ ☺L ♦nodet ▼Lorg/codehaus/jackson/
JsonNode;xp{sr  java.io.NotSerializableException(Vx τå▬5☻  xr ↔java.io.ObjectStr
eamExceptiond├Σkì9√▀☻  xr ‼java.io.IOExceptionlÇsde%≡½☻  xr ‼java.lang.Exception
detailMessaget ↕Ljava/lang/String;[ 5'9w╕╦♥ ♥L ♣causet §Ljava/lang/Throwable;L
stackTracet ▲[Ljava/lang/StackTraceElement;xpq t !org.codehaus.jackson.node.IntN
odeur ▲[Ljava.lang.StackTraceElement;☻F*<<²"9☻  xp    sr ←java.lang.StackTraceEl
ementa  ┼Ü&6▌à☻ ♦I
lineNumberL ♫declaringClassq ~ ♠LfileNameq ~ ♠L
methodNameq ~ ♠xp  ♦ìt →java.io.ObjectOutputStreamt ↨ObjectOutputStream.javat ♀w
riteObject0sq ~ ♀  ♣εt →java.io.ObjectOutputStreamt ↨ObjectOutputStream.javat ↕d
...

1 个答案:

答案 0 :(得分:2)

使用:

<set-payload value="#[message.payload[0]['1']]" />

获取JSON数组中的第一个对象,然后获取名为1的属性的值。

编辑:你也得到了流量错误的处理器顺序。由于您已将set-payload置于response区块中,因此 object-to-json-transformer后会执行,这显然不是您想要的。

我建议你简单地选择:

...
</db:select>
<set-payload value="#[message.payload[0]['1']]" />
<json:object-to-json-transformer />