Mule ESB:将JSON对象转换为另一个对象

Mule ESB:将JSON对象转换为另一个对象,json,mule,esb,jsonpath,Json,Mule,Esb,Jsonpath,我有以下流程: <?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:/

我有以下流程:

<?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(BAL) FROM xxxx.ACCT]]></db:parameterized-query>
        </db:select>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>
</mule>
请注意,444是一个数值(它周围没有引号)

我想做什么

  • 创建一个没有数组的简单JSON结构

  • 将444从数值更改为字符串值

  • 让它看起来像:(将444放入另一个结构)

    {“总计”:“444”,“日期”:“14/07/14”}

我知道要获取系统日期,我需要执行以下操作:

#[server.dateTime.format('dd/MM/yy')]
$..1
。。。我知道要从原始字符串中获取444值,我执行了以下操作:

#[server.dateTime.format('dd/MM/yy')]
$..1
但我不知道下一步该怎么办

既然我使用了一个JSON对象来查看数据库连接器的结果,那么接下来我要做什么来创建我的新结构呢


如果我使用另一个JSON对象,我将如何构造表达式?

要将所有数字写入字符串,您可以在Jackson对象映射器上设置它,并从转换器中引用该自定义对象映射器:

     <spring:beans>
        <spring:bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />

        <spring:bean
            class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <spring:property name="targetObject" ref="jacksonObjectMapper" />
            <spring:property name="targetMethod" value="configure" />
            <spring:property name="arguments">
                <spring:list>
                    <spring:value>WRITE_NUMBERS_AS_STRINGS</spring:value>
                    <spring:value>true</spring:value>
                </spring:list>
            </spring:property>
        </spring:bean>
    </spring:beans>


    <flow name="flow1" doc:name="flow1">
        ... 
        <json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />
        ...
    </flow>

将数字写为字符串
真的
... 
...
但是,这将为所有数字字段添加点。您可能必须为特定行为编写自定义序列化程序

至于展开阵列。不确定您是否可以通过mule使用的jackson版本的对象映射器执行此操作。但是在这种情况下,如果您总是从查询中返回一个结果,那么您可以在json转换器之前展开数组

<set-payload value="#[payload[0]]">
<json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />

一种简单的方法是使用骡子。
您需要从JSON数组中提取值,并将其存储到一些变量中,如下所示:-

<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object" />
<set-variable variableName="Total" value="#[message.payload[0].1]"  doc:name="Variable" />

现在,变量Total将包含值444

下一步是将日期存储到其他变量中,如下所示:-

<set-variable variableName="Date" value="#[server.dateTime.format('dd/MM/yy')]" doc:name="Variable" />

现在,如果完成了这两个步骤,那么您可以使用以下方法以非常简单的方式创建所需的JSON

<expression-transformer
            expression="#[[ 
                'Total': flowVars['Total'].toString(),
                'Date': flowVars['Date']
                        ]]" doc:name="Expression" />
<json:object-to-json-transformer doc:name="Object to JSON" />

这将以一种非常简单的方式生成和构造您所需的JSON:-
{“Date”:“12/08/15”,“Total”:“444”}