如何访问Apachecamel消息体中映射列表中的值



也许这很容易,但不知何故,我还无法破解它。交换的消息主体基本上是一个映射列表,其中包含键&值为字符串。例如,

[{'key'='val1'}, {'key'='val2'},...]

我使用简单表达式将其设置为一个属性,我将在后续路由中使用该属性。这就是我设置它的方式:

.setProperty("myProperty", simple("${body}"))

但这设置了完整的身体。我只想(以某种方式(只设置值部分,以避免设置整个映射列表。到目前为止,我尝试过但没有成功:

.setProperty("myProperty", simple("${body}['key']"))
.setProperty("myProperty", simple("${body}[*]['key']"))
.setProperty("myProperty", simple("${body}[0]['key']")) // this returns only the first value, I want all

有什么想法/建议我该如何实现?

您可以使用简单表达式访问身体的各个级别:

${body} // get whole list of maps
${body[0]} // get first map in the list (index 0)
${body[0][key]} // get value of key "key" from the first map in the list 

在Simple表达式中,不能将数据结构转换为另一个表达式。

然而,您可以简单地将Javabean插入您的路由

from("direct:start")
...
.bean(MyConversionBean.class)
...;

并使用Java 进行转换

public class MyConversionBean {
public List<String> convertBody() {
// extract all values (or whatever) with Java;
return listOfValues;
}
}

最新更新