Type ConverterIts very common when routing messages from one endpoint to another to need to convert the body payloads from one type to another such as to convert to and from the following common types
The Message interface So in an endpoint you can convert a body to another type via Message message = exchange.getIn(); Document document = message.getBody(Document.class); How Type Conversion worksThe type conversion strategy is defined by the TypeConverter The default implementation, DefaultTypeConverter Discovering Type ConvertersThe AnnotationTypeConverterLoader e.g. the following shows how to register a converter from File -> InputStream @Converter public class IOConverter { @Converter public static InputStream toInputStream(File file) throws FileNotFoundException { return new BufferedInputStream(new FileInputStream(file)); } } Static methods are invoked; non-static methods require an instance of the converter object to be created (which is then cached). If a converter requires configuration you can plug in an Injector interface to the DefaultTypeConverter which can construct and inject converter objects via Spring or Guice. We have most of the common converters for common Java types in the org.apache.camel.converter Writing your own Type ConvertersYou are welcome to write your own converters. Remember to use the @Converter annotations on the classes and methods you wish to use. Then add the packages to a file called META-INF/services/org/apache/camel/TypeConverter in your jar. Remember to make sure that :-
|