【发布时间】:2021-09-24 20:21:59
【问题描述】:
我有一个骆驼路线,我在其中使用 Content Enricher EIP。所以我的路线是这样的:
AggregationStrategy aggregationStrategy = new TestAggregationStrategy();
from("timer:myTimer?repeatCount=1")
.setBody(simple("{\"channel\": \"orderbook\", \"market\": \"BTC/USD\", \"type\": \"update\", \"data\": {\"time\": 1626139758.986094, \"checksum\": 195176195, \"bids\": [[32965.0, 0.0], [32962.0, 3.4015]], \"asks\": [], \"action\": \"update\"}}"))
.setHeader(MongoDbConstants.CRITERIA, constant(Filters.eq("market", "BTC/USD")))
.enrich("mongodb:mongo?database=dev&collection=order&operation=findOneByQuery", aggregationStrategy)
.log("Test: ${body}");
在 TestAggregationStrategy 我有两个交换,原始和资源。
public class TestAggregationStrategy implements AggregationStrategy {
public Exchange aggregate(Exchange original, Exchange resource) {
System.out.println("Original: " + original.getMessage().getBody().toString());
String bidsJsonQuery = "$.data.bids";
DocumentContext updateContext = JsonPath.parse(original.getMessage().getBody().toString());
List<String> updateJsonString = updateContext.read(bidsJsonQuery);
System.out.println(resource.getMessage().getBody());
//ArrayList test = (ArrayList) resource.getMessage().getBody(List.class);
Object test = resource.getMessage().getBody();
System.out.println("Test: " + test);
System.out.println("Resource: " + resource.getMessage().getBody());
return resource;
}
}
我可以使用resource.getMessage().getBody() 获取消息内容,它返回某种对象。如果我将它放入 println() 中,它会打印 Mongo 文档。当我检查与调试器的资源交换时,它看起来像这样:
交换 > in > 正文(“文档”)
其中包含一个哈希映射数组。在此示例中,其中一个哈希映射的键为“bids”,其值为数组列表。
您能否直接访问这些值?数组列表?我一直在试验,但无法通过 getBody()。
【问题讨论】:
标签: mongodb spring-boot apache-camel