如何从 Apache 骆驼消息中的地图列表访问值 body
How to access values from list of maps in Apache camel message body
也许这很容易,但我还不能破解它。交换的消息 body 基本上是一个映射列表,键和值都是字符串。例如,
[{'key'='val1'}, {'key'='val2'},...]
我正在使用 简单 表达式将其设置为 属性,我将在后续路由中使用它。这就是我的设置方式:
.setProperty("myProperty", simple("${body}"))
但这样设置就完整了body。我只想(以某种方式)仅设置值部分以避免设置 entire 地图列表。到目前为止我尝试过但没有奏效的方法:
.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
任何 idea/suggestion 我怎样才能做到这一点?
您可以使用 Simple 表达式访问 body 的每个级别:
${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 表达式中做的是在另一个表达式中转换你的数据结构。
但是,您可以简单地将一个 Java bean 插入到您的路由中
from("direct:start")
...
.bean(MyConversionBean.class)
...;
并使用 Java
进行转换
public class MyConversionBean {
public List<String> convertBody() {
// extract all values (or whatever) with Java;
return listOfValues;
}
}
也许这很容易,但我还不能破解它。交换的消息 body 基本上是一个映射列表,键和值都是字符串。例如,
[{'key'='val1'}, {'key'='val2'},...]
我正在使用 简单 表达式将其设置为 属性,我将在后续路由中使用它。这就是我的设置方式:
.setProperty("myProperty", simple("${body}"))
但这样设置就完整了body。我只想(以某种方式)仅设置值部分以避免设置 entire 地图列表。到目前为止我尝试过但没有奏效的方法:
.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
任何 idea/suggestion 我怎样才能做到这一点?
您可以使用 Simple 表达式访问 body 的每个级别:
${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 表达式中做的是在另一个表达式中转换你的数据结构。
但是,您可以简单地将一个 Java bean 插入到您的路由中
from("direct:start")
...
.bean(MyConversionBean.class)
...;
并使用 Java
进行转换public class MyConversionBean {
public List<String> convertBody() {
// extract all values (or whatever) with Java;
return listOfValues;
}
}