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.Ce contenu n'est pas disponible dans la langue sélectionnée.
45.4. Implementing a Type Converter Directly
Overview Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Generally, the recommended way to implement a type converter is to use an annotated class, as described in the previous section, Section 45.3, “Implementing Type Converter Using Annotations”. But if you want to have complete control over the registration of your type converter, you can implement a custom slave type converter and add it directly to the type converter registry, as described here.
Implement the TypeConverter interface Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
To implement your own type converter class, define a class that implements the
TypeConverter interface. For example, the following MyOrderTypeConverter class converts an integer value to a MyOrder object, where the integer value is used to initialize the order ID in the MyOrder object.
Add the type converter to the registry Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
You can add the custom type converter directly to the type converter registry using code like the following:
// Add the custom type converter to the type converter registry context.getTypeConverterRegistry().addTypeConverter(MyOrder.class, String.class, new MyOrderTypeConverter());
// Add the custom type converter to the type converter registry
context.getTypeConverterRegistry().addTypeConverter(MyOrder.class, String.class, new MyOrderTypeConverter());
Where
context is the current org.apache.camel.CamelContext instance. The addTypeConverter() method registers the MyOrderTypeConverter class against the specific type conversion, from String.class to MyOrder.class.
You can add custom type converters to your Camel applications without having to use the
META-INF file. If you are using Spring or Blueprint, then you can just declare a <bean>. CamelContext discovers the bean automatically and adds the converters.
<bean id="myOrderTypeConverters" class="..."/> <camelContext> ... </camelContext>
<bean id="myOrderTypeConverters" class="..."/>
<camelContext>
...
</camelContext>
You can declare multiple <bean>s if you have more classes.