我对 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/