如何遍历 Camel 体内的嵌套列表?

How can I iterate over a nested list inside Camel body?

我正在尝试访问传入的 {body} 中的数据 Json 我已经用 Jackson 完成了解组并将其映射到 Java 地图 Class

  `.unmarshal().json(JsonLibrary.Jackson, java.util.Map.class)
`

我传入的 Json 数据在上面的解组步骤后是这样的

{ "projectId" : 12345,
"title" : “12345 - Plant 1 Processing",
"partners": [{"partnerName": "partnerJV1", "partnerLocation": "JA"},
{"partnerName": "partnerJV2", "partnerLocation": "FL"},
{"partnerName": "partnerJV3", "partnerLocation": "OH"}
]

合作伙伴字段可以有 0-N 个 partnerName、partnerLocation 映射。

现在我将其插入 SQL table 和

.to("sql:classpath:sql/sql_queries.sql")

我的 sql_queries.sql 中有以下查询,用于将数据字段插入 table:

INSERT INTO MY_TABLE(PID, TITLE, PartnerName1, PartnerLocation1, PartnerName2, PartnerLocation2, PartnerName3, PartnerLocation3) VALUES(:#${body['projectId']}, :#${body['title']}, :#${body['partners[0]']['partnerName']}, :#${body['partners[0]']['partnerLocation']} )

我的问题是我不知道合作伙伴的确切数量,没有它我无法写我的 SQL 声明。如果我访问说,我得到 IndexOutOfBounds 异常:#${body['partners'][2]['partnerName']}

但是来体中只有一个合作伙伴

那么我如何在 camel 中根据它的长度迭代我的 JSON 中的嵌套映射并为我的插入语句初始化我的 PartnerName、PartnerLocation 字段?

这样试试:

            .setProperty("projectId", simple("body['projectId']"))
            .setProperty("title", simple("body['title']"))
            .setBody(simple("body['partners']"))
            .split(simple("body"))
            .process{//prepare your properties here}
            .end()
            .to("sql:classpath:sql/sql_queries.sql");

而 sql 将如下所示:

INSERT INTO MY_TABLE(PID, TITLE, PartnerName1, PartnerLocation1, PartnerName2, PartnerLocation2, PartnerName3, PartnerLocation3) VALUES(:#${exchangeProperty.projectId}, :#${exchangeProperty.title}, :#${exchangeProperty.partnerName1}, :#${exchangeProperty.partnerLocation1} , :#${exchangeProperty.partnerName2}, :#${exchangeProperty.partnerLocation2}, :#${exchangeProperty.partnerName3}, :#${exchangeProperty.partnerLocation3}   )

UPD:对于 1 行中的所有数据

最后做了这样的事情:

               .process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    Map<String, Object> body = (Map<String, Object>) exchange.getIn().getBody();
                    int i = 1;
                    for(Map entry : (List<Map>)body.get("partners"))

                            {

                              exchange.setProperty("PartnerName"+i, entry.get("partnerName"));
                              exchange.setProperty("PartnerLocation"+i, entry.get("partnerLocation"));

                              i++;
                       }

然后在 INSERT INTO (...PartnerName1, PartnerLocation1, PartnerName2, PartnerLocation2.....) VALUES (....:#${属性.PartnerName1}, :#${属性.PartnerLocation1}...)