简体   繁体   English

Apache骆驼中的类型转换

[英]type conversion in apache camel

I am pretty new to Apache Camel and currently I am making a test project. 我对Apache Camel并不陌生,目前正在做一个测试项目。 Currently I am trying to write some previously processed objects from custom class into a file using the file component (i dont know a better option). 目前,我正在尝试使用文件组件将自定义类中的某些先前处理的对象写入文件(我不知道更好的选择)。

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

but i have the following problem 但我有以下问题

   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}]

I saw on the internet that some people suggests writing custom TypeConverter and then register it in a file in the WEB-INF folder. 我在互联网上看到有人建议编写自定义TypeConverter,然后将其注册到WEB-INF文件夹中的文件中。 But I am using SpringBoot and its internal Tomcat and I dont have this directory. 但是我正在使用SpringBoot及其内部的Tomcat,但没有此目录。

You can convert body to String with convertBodyTo(String.class) , it will use toString method on Your object to make a conversion: 您可以使用convertBodyTo(String.class)将body转换为String,它将在对象上使用toString方法进行转换:

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

This should write 'Add{x=5.63, y=78.016}' into the file. 这应将“ Add {x = 5.63,y = 78.016}”写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM