在我的一个用例中,我需要根据消息中可用的数组列表将入站json消息拆分为多个消息。
例如,在下面要基于"actualData“数组列表进行的消息拆分中。
{
"information": {
"name": "ObjectName",
"type": "messageType"
},
"actualData": [
{
"msg": "message-1"
},
{
"msg": "message-2"
}
]
}完成拆分后,在发送到下一条路由之前,需要在每条消息中添加"information“对象。这项工作需要在XML本身中完成,而不是使用任何java处理器或java代码。
预期的结果需要如下:
拆分消息:1
{
"information": {
"name": "ObjectName",
"type": "messageType"
},
"actualData":
{
"msg": "message-1"
}
}拆分消息:2
{
"information": {
"name": "ObjectName",
"type": "messageType"
},
"actualData":
{
"msg": "message-2"
}
}问题陈述:
我能够基于"actualData“数组对象拆分消息,但是当我试图在最终的exchange主体中添加附加的对象时,使用jsonpath将json对象转换为string会遇到一些问题。
下面是为每一条信息调用的路线。
<route id="split_message">
<from uri="direct:split_message_onebyone"/>
<log message="Sending message body to split message one by one : ${body} "/>
<convertBodyTo type="java.lang.String"/>
<setProperty name="information">
<jsonpath resultType="String" writeAsString="true">$.information</jsonpath>
</setProperty>
<log message="printing property after string message body to split message one by one : ${exchangeProperty.information}} "/>
<split streaming="true">
<jsonpath>$.actualData</jsonpath>
<marshal> <custom ref="jack" /> </marshal>
<setBody>
<simple>{{"information" : ${exchangeProperty.information}}, "actualData" : ${body}, } </simple>
</setBody>
<convertBodyTo type="java.lang.String"/>
</split>
<log message="split end one by one ${body}"/>
</route> 我在这里尝试的方法是,在实际发生拆分之前,从原始消息中获取"information“对象,并将其存储到名为"information”的exchange属性中,然后在拆分发生后,从exchange引用该属性,并准备在其中使用"information“+ "actualData”对象创建json的主体。
一切都按预期工作,但这里的问题是,当"information“对象存储在property字段中时,它不是作为字符串存储,而是按下面的存储方式存储。
{
name: "ObjectName",
type: "messageType"
}预期存储如下。
{
"name": "ObjectName",
"type": "messageType"
}由于这个问题,最终的正文消息不会变成json字符串。有人能帮忙解决这个问题吗?提前谢谢。我用的是骆驼3.4版。
发布于 2022-11-03 07:38:40
在响应后执行编组,而不是转换为字符串。
https://stackoverflow.com/questions/64518083
复制相似问题