【问题标题】:type conversion in apache camelapache骆驼中的类型转换
【发布时间】:2019-02-22 15:21:09
【问题描述】:

我对 Apache Camel 很陌生,目前我正在制作一个测试项目。目前我正在尝试使用文件组件将一些以前处理过的对象从自定义类写入文件(我不知道更好的选择)。

from("direct:processedDecimals")
            .to("file:data/output")

但我有以下问题

   Caused by: org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: Add{x=5.63, y=78.016} of type: org.example.math.Add on: Message[ID-NTB828-1537281187742-0-10]. Caused by: No type converter available to convert from type: org.example.math.Add to the required type: java.io.InputStream with value Add{x=5.63, y=78.016}. Exchange[ID-NTB828-1537281187742-0-9]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.example.math.Add to the required type: java.io.InputStream with value Add{x=5.63, y=78.016}]

我在网上看到有人建议编写自定义 TypeConverter,然后将其注册到 WEB-INF 文件夹中的文件中。但是我使用的是 SpringBoot 及其内部的 Tomcat,我没有这个目录。

【问题讨论】:

    标签: java apache spring-boot apache-camel integration


    【解决方案1】:

    您可以使用 convertBodyTo(String.class) 将 body 转换为 String,它会在您的对象上使用 toString 方法进行转换:

    from("direct:processedDecimals")
        .convertBodyTo(String.class)
        .to("file:data/output");
    

    这应该将 'Add{x=5.63, y=78.016}' 写入文件。

    【讨论】:

    • 你是对的。我做了类似的事情,我不再面临同样的问题。谢谢
    猜你喜欢
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多