【问题标题】:In Mule ESB's ALL control flow, how to merge two resulting JSON collections into a single list of JSON values?在 Mule ESB 的 ALL 控制流中,如何将两个生成的 JSON 集合合并为一个 JSON 值列表?
【发布时间】:2014-05-14 17:05:01
【问题描述】:

我有一个 3.4.0 Mule 流(xml 显示在末尾),它使用 ALL 流控制向两个不同的远程 RESTful Web 服务(美国和欧盟)发送相同的查询,这两个服务都返回 JSON 列表价值观。我的问题是 ALL 流控制返回 2 个 JSON 值列表的列表,而不是我想要的(JSON 值的单个合并列表)。即我得到这个:

[[
{
    "StudyData": {
        "@id": "0e521e697c1f76f1603a21fff98dbeb5",
        "@xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
        "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "Base-Scanning-Parameters": {
            "Scanning": {
                "exposureMultiplier": "7"
            }
        },
        "Calibration": {
            "Directory": "D:\\Data\\CASE_00283\\MicroCal",
            "GUID": null,
            "Version": "2"
        },
        "Imager": {
            "DeviceId": "DEV000001",
            "Model": "DPI Dpca",
            "SerialNumber": "Apr 16 2012",
            "SystemSerialNumber": "DEV000001",
            "Version": "1.1.0"
        }
    }
}
], [
{
    "StudyData": {
        "@id": "59130b92f421ad1c0b2e610420c8bc16",
        "Base-Scanning-Parameters": {
            "Scanning": {
                "exposureMultiplier": "7"
            }
        },
        "Calibration": {
            "Directory": "D:\\Data\\CASE_00283\\MicroCal",
            "GUID": null,
            "Version": "2"
        },
        "Imager": {
            "DeviceId": "TestDevice",
            "Model": "DPI Dpca",
            "SerialNumber": "Apr 16 2012",
            "SystemSerialNumber": "1234567A",
            "Version": "1.1.0"
        }
    }
},
{
    "StudyData": {
        "@id": "7ae4c6840ef7bfa1b2f5f17290f78bcd",
        "Base-Scanning-Parameters": {
            "Scanning": {
                "exposureMultiplier": "7"
            }
        },
        "Calibration": {
            "Directory": "D:\\Data\\CASE_00283\\MicroCal",
            "GUID": null,
            "Version": "2"
        },
        "Imager": {
            "DeviceId": "TestDevice",
            "Model": "DPI Dpca",
            "SerialNumber": "Apr 16 2012",
            "SystemSerialNumber": "1234567A",
            "Version": "1.1.0"
        }
    }
}
]]

我需要的是两个“StudyData”列表合并到一个列表中(我删除了空间的记录详细信息):

[
  {"StudyData": { ...
  },
  {"StudyData": { ...
  },
  {"StudyData": { ...
  }
]

此流程的 Mule XML 是:

-------------- 启动 Mule XML: ----------------------------- ---

<flow name="federatedWebService" doc:name="federatedWebService">
    <copy-properties propertyName="http.method" doc:name="Copy http.method"/>
    <choice doc:name="Choice">
        <when expression="#[message.inboundProperties['http.method']=='GET']">
            <all doc:name="All">
                <processor-chain>
                    <logger message="Redirected GET to US, path is: #[path]" level="DEBUG" category="com.dpi.integration.federatedWebService" doc:name="Redirecting GET to US Path"/>
                    <https:outbound-endpoint exchange-pattern="request-response" host="${http.outbound.us.host}" port="${http.outbound.us.port}" path="#[path]" method="GET" followRedirects="true" connector-ref="HTTPSConnector" doc:name="US GET"/>
                    <set-variable variableName="USStatus" value="#[message.inboundProperties['http.status']]" doc:name="Set UStatus"/>
                    <logger message="US Response: #[USStatus]" level="DEBUG" category="com.dpi.integration.federatedWebService" doc:name="US Response"/>
                    <object-to-string-transformer doc:name="Object to String"/>
                    <logger message="US Response: #[message.payload]" level="INFO" doc:name="Logger"/>
                </processor-chain>
                <processor-chain>
                    <logger message="Redirected GET to EU, path is: #[path]" level="DEBUG" category="com.dpi.integration.federatedWebService" doc:name="Redirecting GET to EU Path"/>
                    <https:outbound-endpoint exchange-pattern="request-response" host="${http.outbound.eu.host}" port="${http.outbound.eu.port}" path="#[path]" method="GET" followRedirects="true" connector-ref="HTTPSConnector" doc:name="EU GET"/>
                    <set-variable variableName="EUStatus" value="#[message.inboundProperties['http.status']]" doc:name="Set EUStatus"/>
                    <logger message="EU Response: #[EUStatus]" level="DEBUG" category="com.dpi.integration.federatedWebService" doc:name="EU Response"/>
                    <object-to-string-transformer doc:name="Object to String"/>
                    <logger message="EU Response:  #[message.payload]" level="DEBUG" doc:name="Logger"/>
                </processor-chain>
            </all>
        </when>
        <otherwise>
            <logger message="Redirected #['http.method'] to US" level="DEBUG" category="com.dpi.integration.federatedWebService" doc:name="Redirected to US"/>
            <http:outbound-endpoint exchange-pattern="request-response" host="${http.outbound.us.host}" port="${http.outbound.us.port}" followRedirects="true" doc:name="US All Else"/>
        </otherwise>
    </choice>
    <object-to-string-transformer encoding="UTF-8" mimeType="application/json" doc:name="Object to String"/>
    <logger message="Response: #[message.payload]" level="DEBUG" category="com.dpi.integration.federatedWebService" doc:name="Response"/>
    <catch-exception-strategy doc:name="Catch Exception Strategy">
        <set-property propertyName="http.status" value="#[(USStatus == 404) ? EUStatus : USStatus]" doc:name="Set HTTP Status to USStatus"/>
        <choice doc:name="Choice">
            <when expression="#[USStatus == 404]">
                <set-payload value="Unable to locate the requested resource: #[message.inboundProperties['http.request.path']]" doc:name="404 Message"/>
            </when>
            <otherwise>
                <set-payload value="Unable to process the requested resource: #[message.inboundProperties['http.request.path']]" doc:name="Generic Error Message"/>
            </otherwise>
        </choice>
        <logger message="Unable to process the requested resource: #[message.inboundProperties['http.request.path']], US HTTP Status: #[USStatus], EU HTTP Status: #[EUStatus]" level="ERROR" category="com.dpi.integration.federatedWebService" doc:name="Error Message"/>
    </catch-exception-strategy>
</flow>

----------- end mule XML -------

任何建议将不胜感激。

谢谢!!

-基思

【问题讨论】:

    标签: json list merge mule esb


    【解决方案1】:

    您可以在两个 JSON 值上使用 &lt;json:json-to-object-transformer returnClass="java.util.List"/&gt;,然后使用 Groovy 使用 payload.flatten() 展平列表列表。

    【讨论】:

    • 安东,感谢您的回复。我会尝试一下并在这里评论结果。谢谢!
    • 是的!这解决了问题。非常感谢安东。
    猜你喜欢
    • 2018-10-25
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多