1

我有以下流程:

<?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>

流词很好,正如您所见,JSON 对象将 Database 对象转换为以下内容:(带有单个对象的 JSON 数组):

[{"1":444}]

请注意,444 是一个数值(它周围没有引号)。

我想做的事

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

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

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

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

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

#[server.dateTime.format('dd/MM/yy')]

...而且我知道要从原始字符串中获取 444 值,我执行了以下操作:

$..1

但我不知道下一步该做什么。

现在我使用了 JSON 对象来查看数据库连接器的结果,接下来我要做什么对象来创建我的新结构。

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

4

2 回答 2

2

要将所有数字写为字符串,您可以在 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>

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

至于解开数组。不确定您是否可以通过带有杰克逊版本骡子使用的对象映射器来执行此操作。但是对于这种情况,如果您总是从查询中得到一个结果 - 您可能只是在 json 转换器之前展开数组

<set-payload value="#[payload[0]]">
<json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />
于 2014-07-14T21:46:27.183 回答
0

一个简单的方法是使用 Mule 的Expression Transformer
您需要从 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" />

现在,如果完成了这两个步骤,那么您可以使用Expression Transformer以非常简单的方式创建所需的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"}以一种非常简单的方式

于 2015-08-12T12:24:16.993 回答