0

早上好!我正在尝试使用 Mule 创建一个 Web API,它将一个基于 JSON 的请求从系统 A 转换为另一种基于 JSON 的格式,使用转换后的 JSON 在系统 B 上调用 Web API,从系统 B 获取 JSON 响应并转换它是系统 A 可以理解的一些 JSON,并将该结果返回到对原始 HTTP 请求的响应中。我大部分时间都在工作,但我不知道如何在属性中引用数组。这是进入Expression组件的 JSON:

{
    "FooObjects":
    [
        {
            "FooPropertyOne": "value for property one"
          , "FooPropertyTwo": "value for property two"
          , "FooPropertyThree": "value for property three"
          , "FooId": 22031
          , "FooBarRelationships":
            [
                {
                    "FooBarRelationshipId": 18014
                  , "RelationshipProperty": "value for rel property"
                  , "Bar": { "BarPropertyOne": "value for property one", "BarId": 11379 }
                }
              , {
                    "FooBarRelationshipId": 18015
                  , "RelationshipProperty": "value for rel property"
                  , "Bar": { "BarPropertyOne": "value for property one", "BarId": 10001 }
                }
            ]
        }
    ]
}

这是我想要的结果:

{
    "FooObjects":
    [
        {
            "FooPropertyOne": "value for property one"
          , "FooPropertyTwo": "value for property two"
          , "FooPropertyThree": "value for property three"
          , "FooId": 22031
          , "FooUrl": "https://server/path/to/foo?FooId=22013"
          , "Bars":
            [
                { "BarPropertyOne": "value for property one", "BarId": 11379 }
              , { "BarPropertyOne": "value for property one", "BarId": 10001 }
            ]
        }
    ]
}

这是表达式组件:

    <expression-component doc:name="Expression"><![CDATA[payload =
[
    "FooObjects": ([
        "FooPropertyOne": $.FooPropertyOne
      , "FooPropertyTwo": $.FooPropertyTwo
      , "FooPropertyThree": $.FooPropertyThree
      , "FooId": $.FooId
      , "FooURl": "https://server/path/to/foo?FooId=" + $.FooId
      , "Bars": $.FooBarRelationships
    ] in payload.FooObjects)
];]]></expression-component>

我一直在猜测要代替$.FooBarRelationships. 我能想到的最好的方法是(['BarPropertyOne':$.Bar.BarPropertyOne]in $.FooBarRelationships). 这让我:

[Error: could not access: Bar; in class: java.util.LinkedHashMap]
[Near : {... $.Bar.BarPropertyOne....}]
             ^

如何执行此转换?如果我省略关于 的部分Bars,除了没有获得我需要的所有数据外,一切正常。谢谢!

编辑:我更改了示例以更清楚地显示多样性。

4

1 回答 1

1

我能够完成这项工作的唯一方法是通过一个函数:

<configuration>
    <expression-language>
        <global-functions>
            def makeBars(fooBarRelationships) {
              (['BarPropertyOne':$.Bar.BarPropertyOne] in fooBarRelationships)
            }
        </global-functions>
    </expression-language>
</configuration>

然后在流程中我可以使用:

<expression-component doc:name="Expression"><![CDATA[payload =
    [
        "FooObjects": ([
            "FooPropertyOne": $.FooPropertyOne
          , "FooPropertyTwo": $.FooPropertyTwo
          , "FooPropertyThree": $.FooPropertyThree
          , "FooId": $.FooId
          , "FooURl": "https://server/path/to/foo?FooId=" + $.FooId
          , "Bars": makeBars($.FooBarRelationships)
        ] in payload.FooObjects)
    ];]]></expression-component>
于 2013-12-05T06:12:08.630 回答