java - Apache Camel 中的类型转换

标签 java apache spring-boot apache-camel integration

我对 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,并且我没有这个目录。

最佳答案

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

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

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

关于java - Apache Camel 中的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52389110/

相关文章:

Java 3DES 实现(学术)

Java:如何在重写方法(祖 parent 方法)中调用 super().super()

apache - 如何在 mod_auth_cas 中支持 "require group foobar"

php - 处理同一 URL 中的多个版本

java - 无法将 SpringBoot 项目导入 Eclipse IDE

java - 如何在 Java 中扩展不可变类型

Java 8 : Difference between method reference Bound Receiver and UnBound Receiver

html - firefox 不加载外部字体

java - Bitronix 事务无法与 JMS 队列一起使用

java - 将带有 jackson 的嵌套 json 映射到 java 字段