The type conversion mechanism can easily be customized by adding a new slave type converter. This section describes how to implement a slave type converter and how to integrate it with FUSE Mediation Router, so that it is automatically loaded by the annotation type converter loader.
You can implement a custom type converter class using the
@Converter
annotation. You must annotate the class itself
and each of the methods intended to perform type conversion. Each converter method must
take a single argument, which defines the from type, and a non-void
return value, which defines the to type. The type converter loader
uses Java reflection to find the annotated methods and integrate them into the type
converter mechanism. Example 3.3 shows an example of an
annotated converter class that defines a single converter method for converting from
java.io.File
to java.io.InputStream
.
Example 3.3. Example of an Annotated Converter Class
package com.YourDomain
.YourPackageName
; import org.apache.camel.Converter; import java.io.*; @Converter public class IOConverter { private IOConverter() { } @Converter public static InputStream toInputStream(File file) throws FileNotFoundException { return new BufferedInputStream(new FileInputStream(file)); } }
The toInputStream()
method is responsible for performing the
conversion from the File
type to the InputStream
type.
![]() | Note |
---|---|
The method name is unimportant, and can be anything you choose. What is important
are the argument type, the return type, and the presence of the
|
To enable the discovery mechanism (which is implemented by the annotation
type converter loader) for your custom converter, create a
TypeConverter
file at the following location:
META-INF/services/org/apache/camel/TypeConverter
The TypeConverter
file must contain a comma-separated list of package names
identifying the packages that contain type converter classes. For example, if you want the
type converter loader to search the
com.
YourDomain
.
YourPackageName
package for annotated converter classes, the TypeConverter
file would have the
following contents:
com.YourDomain
.YourPackageName