Camel 将Json解组为bean方法上的参数

5vf7fwbs  于 2023-01-05  发布在  Apache
关注(0)|答案(1)|浏览(106)

在调用时将消息正文解组到pojo中,我遇到了一个非常糟糕的问题。
我已经定义了一个非常简单的路由,将body解组到我的自定义pojo中:

<dataFormats>
        <json id="json" library="Jackson" unmarshalTypeName="com.trumin.domain.model.result.Result" />
    </dataFormats>

    <camel:route>
        <camel:from
            uri="activemq:topic:result?clientId=sswric_01&amp;durableSubscriptionName=sendSMSWhenResultIsCalculated" />
        <marshal ref="json" />
        <bean beanType="com.trumin.communications.sms.TimeResultSMSSender"
            method="sendTextToUserAfterTimeResultBeingSaved(${body})" />
    </camel:route>
</camelContext>

我现在要调用的方法

sendTextToUserAfterTimeResultBeingSaved(Result r);

接收在json数据格式中定义的Result类型的参数,并且在消息被传递时不调用该参数。
如果我修改方法签名以接受String而不是Result:

sendTextToUserAfterTimeResultBeingSaved(String s);

那么它被调用并且该串包含结果JSON表示。
有没有什么方法能让我努力实现的目标奏效?
我也尝试过从路由中删除(${body})规范,我尝试过其他解组库,我尝试过阅读所有camel教程和参数绑定链接,我阅读了camel in action中关于这个问题的大部分章节,但仍然找不到答案。

bwleehnv

bwleehnv1#

最后...这个修好了:

<camelContext xmlns="http://camel.apache.org/schema/spring">

    <camel:dataFormats>
        <camel:json id="jsonToResult" library="Jackson" unmarshalTypeName="com.trumin.domain.model.result.Result" />
    </camel:dataFormats>

    <camel:route>
        <camel:from
            uri="activemq:topic:result?clientId=sswric_01&amp;durableSubscriptionName=sendSMSWhenResultIsCalculated" />
        <camel:unmarshal ref="jsonToResult" />
        <camel:bean beanType="com.trumin.communications.sms.TimeResultSMSSender"
            method="sendTextToUserAfterTimeResultBeingSaved" />
    </camel:route>
</camelContext>

相关问题