Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Este conteúdo não está disponível no idioma selecionado.
45.3. Implementing Type Converter Using Annotations
Overview Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 Apache Camel, so that it is automatically loaded by the annotation type converter loader.
How to implement a type converter Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
To implement a custom type converter, perform the following steps:
Implement an annotated converter class Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
You can implement a custom type converter class using the
@Converter annotation. You must annotate the class itself and each of the static methods intended to perform type conversion. Each converter method takes an argument that defines the from type, optionally takes a second Exchange argument, and has a non-void return value that 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 45.3, “Example of an Annotated Converter Class” shows an example of an annotated converter class that defines a converter method for converting from java.io.File to java.io.InputStream and another converter method (with an Exchange argument) for converting from byte[] to String.
Example 45.3. Example of an Annotated Converter Class
The
toInputStream() method is responsible for performing the conversion from the File type to the InputStream type and the toString() method is responsible for performing the conversion from the byte[] type to the String 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
@Converter annotation.
Create a TypeConverter file Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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
META-INF/services/org/apache/camel/TypeConverter
The
TypeConverter file must contain a comma-separated list of Fully Qualified Names (FQN) of type converter classes. For example, if you want the type converter loader to search the YourPackageName.YourClassName package for annotated converter classes, the TypeConverter file would have the following contents:
com.PackageName.FooClass
com.PackageName.FooClass
An alternative method of enabling the discovery mechanism is to add just package names to the
TypeConverter file. For example, the TypeConverter file would have the following contents:
com.PackageName
com.PackageName
This would cause the package scanner to scan through the packages for the
@Converter tag. Using the FQN method is faster and is the preferred method.
Package the type converter Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
The type converter is packaged as a JAR file containing the compiled classes of your custom type converters and the
META-INF directory. Put this JAR file on your classpath to make it available to your Apache Camel application.
Fallback converter method Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
In addition to defining regular converter methods using the
@Converter annotation, you can optionally define a fallback converter method using the @FallbackConverter annotation. The fallback converter method will only be tried, if the master type converter fails to find a regular converter method in the type registry.
The essential difference between a regular converter method and a fallback converter method is that whereas a regular converter is defined to perform conversion between a specific pair of types (for example, from
byte[] to String), a fallback converter can potentially perform conversion between any pair of types. It is up to the code in the body of the fallback converter method to figure out which conversions it is able to perform. At run time, if a conversion cannot be performed by a regular converter, the master type converter iterates through every available fallback converter until it finds one that can perform the conversion.
The method signature of a fallback converter can have either of the following forms:
Where MethodName is an arbitrary method name for the fallback converter.
For example, the following code extract (taken from the implementation of the File component) shows a fallback converter that can convert the body of a
GenericFile object, exploiting the type converters already available in the type converter registry: