【问题标题】:Camel bean binding tries to convert the body to ExchangeCamel bean binding 尝试将 body 转换为 Exchange
【发布时间】:2016-04-19 23:11:51
【问题描述】:

我有一条简单的路线,如下所示:

<route handleFault="true" streamCache="true" id="routeA">
    <from uri="cxfrs://bean://simpleCxf" />
    <log message="The message body contains ${body}"/>
    <to uri="direct-vm:RouteB" />
</route>

<route handleFault="true" streamCache="true" id="routeB">
    <from uri="direct-vm:RouteB" />
    <bean ref="requestValidator" method="validateRequest" />
    <log message="The input message ${body}" />
    <bean ref="dbClient" method="queryDatabase" />
</route>

cxf 配置也很简单:

<cxf:rsServer id="simpleCxf" address="/test"
    loggingFeatureEnabled="true"
    serviceClass="com.gogol.test.TestResource">
</cxf:rsServer>

这个简单的路由失败了,下面的异常

No type converter available to convert from type: org.apache.cxf.message.MessageContentsList to the required type: org.apache.camel.Exchange with value [com.gogol.test.resource.SimpleObject@773736ca]

这是消息历史记录,表明它在&lt;bean ref="requestValidator" method="validateRequest" /&gt; 处失败

Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId                   Processor                      Elapsed (ms)
[routeA] [routeA] [direct-vm://routeA                                ] [6]
[routeB] [log12 ] [log                                               ] [2]
[routeB] [to9   ] [direct-vm:routeA                                  ] [4]
[routeA] [bean26] [bean[ref:requestValidator method: validateRequest]] [2]

我认为问题在于 Camel 试图将 cxf 生成的主体转换为 Exchange 对象。因为 requestValidator 类有一个方法,它的签名是:

public void validateRequest(Exchange exchange) thows SomeException.

但理想情况下,由 cxf 生成的消息应该设置为 Exchange 内部的正文。我是否正确,如果不是,那么上述异常的原因可能是什么?

编辑:

我使用的是 CXF 版本 3.0.4.redhat-621084 和 CAMEL 版本 2.15.1.redhat-621084

{Caused by: org.apache.camel.InvalidPayloadException: No body available of type: org.apache.camel.Exchange but has value: [com.gogol.schema.TestResourcec@6b651b67] of type: org.apache.cxf.message.MessageContentsList on: Message: [com.gogol.schema.TestResourcec@6b651b67]. Caused by: No type converter available to convert from type: org.apache.cxf.message.MessageContentsList to the required type: org.apache.camel.Exchange with value [com.gogol.schema.TestResourcec@6b651b67]. Exchange[Message: [com.gogol.schema.TestResourcec@6b651b67]]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.apache.cxf.message.MessageContentsList to the required type: org.apache.camel.Exchange with value [com.gogol.schema.TestResourcec@6b651b67]]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:101)[198:org.apache.camel.camel-core:2.15.1.redhat-621084]
    at org.apache.camel.builder.ExpressionBuilder$42.evaluate(ExpressionBuilder.java:1037)[198:org.apache.camel.camel-core:2.15.1.redhat-621084]
    ... 68 more
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.apache.cxf.message.MessageContentsList to the required type: org.apache.camel.Exchange with value [com.gogol.schema.TestResourcec@6b651b67]
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:177)[198:org.apache.camel.camel-core:2.15.1.redhat-621084]
    at org.apache.camel.core.osgi.OsgiTypeConverter.mandatoryConvertTo(OsgiTypeConverter.java:122)[203:org.apache.camel.camel-spring:2.15.1.redhat-621084]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99)[198:org.apache.camel.camel-core:2.15.1.redhat-621084]
        ... 69 more}

【问题讨论】:

    标签: apache-camel cxf


    【解决方案1】:

    我不肯定骆驼 xml dsl 如何正确格式化它,但您始终可以明确告诉骆驼进行类型转换。您可以通过使用 convertBodyTo 调用来强制进行类型转换来测试您的理论。

    from("myEndpoint")
        .log("This is my previous object")
        .convertBodyTo(TestResource.class)
        .log("This is my object after a camel type conversion");
    

    【讨论】:

    • 我已尝试转换您提到的方式,但唯一的区别是在异常中将 MessageContentsList 替换为 TestResource。如果没有 convertBodyTo 方法,则 excpetion 说:没有类型转换器可用于从类型 MessageContentsList 转换为所需的类型:交换使用 convertBodyTo 方法,异常说:没有类型转换器可用于从类型 TestResource 转换 到所需的类型:Exchange。
    • 好的,如果你尝试过,那么看起来你可能会遇到骆驼的问题,认为它需要一个类型转换器来将 MessageContentsList 转换为交换,但因为这应该只是你交换的主体不应该是类型转换。您可以将骆驼上下文设置为 TRACE 并添加显示输出吗?
    • 哪些对象控制着骆驼的思维?根据骆驼的 bean 绑定策略,不应将 Exchange 视为任何其他类进行数据转换。此外,如果我将 validateRequest 方法的参数更改为 TestResource,那么骆驼调用该方法就好了。但后来我没有办法设置交换体。请问有什么想法吗?
    • 如果您只是想替换交易所的正文,那么只需返回一个对象,camel 会自动为您设置现有交易所的 IN 消息正文。至于为什么它试图强制类型转换来交换我仍然不确定。这可能是一个错误,但这必须以最简单的形式复制,然后打开 Jira
    猜你喜欢
    • 2013-10-22
    • 1970-01-01
    • 2019-08-17
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多