Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

So in an endpoint you can convert a body to another type via

...

How Type Conversion works

...

To access the registry, you get it from the CamelContext

...

...

TypeConverterRegistry utilization statistics

...

From Camel 2.11.0/2.10.5 onwards these statistics are turned off by default as there is some performance overhead under very high concurrent load. To enable the statistics in Java, do the following:

...

...

Or in the XML DSL with:

...

...

Add type converter at runtime

The following sample demonstrates how to add a type converter at runtime: Wiki Markup{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryTest.java}And our type converter is implemented as: Wiki Markup{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryTest.java}And then we can convert from String to MyOrder as we are used to with the type converter: Wiki Markup{snippet:id=e3|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryTest.java}

Add type converter classes at runtime

...

From Camel 2.16 onwards you type converter classes can implement org.apache.camel.TypeConverters which is an marker interface. Then for each type converter you want use the @Converter annotation.

...

...

 

Then you can add these converters to the registry using

...

...

If you are using Spring or Blueprint, then you can just declare a <bean> then CamelContext will automatic discover and add the converters.

...

You can declare multiple <bean>s if you have more classes.

...

e.g. the following shows how to register a converter from File -> InputStream

...

...

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.

...

By default when using a method in a POJO annotation with @Converter returning null is not a valid response. If null is returned, then Camel will regard that type converter as a miss, and prevent from using it in the future. If null should be allowed as a valid response, then from Camel 2.11.2/2.12 onwards you can specify this in the annotation as shown:

...

Discovering Fallback Type Converters

...

Fallback type converters are used as a last resort for converting a given value to another type. Its used when the regular type converters give up.
The fallback converters is also meant for a broader scope, so its method signature is a bit different:

...

...

Or you can use the non generic signature.

...

...

And the method name can be anything (convertTo is not required as a name), so it can be named convertMySpecialTypes if you like.
The Exchange parameter is optional, just as its with the regular @Converter methods.

...

For instance in the method below we will handle all type conversions that is based on the wrapper class GenericFile and we let Camel do the type conversions on its body instead.

...

...

Writing your own Type Converters

 

...

...

In Camel 2.8 the TypeConverter file now supports specifying the FQN class name. This is recommended to be used. See below for more details. Using FQN must be used. The older way with just package name is deprecated and should not be used, and it may also not work in some application servers due to classpath scanning issues.

 

You 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 :-

...

The file in the JAR: META-INF/services/org/apache/camel/TypeConverter contains the following line(s)

...

...

Each line in the file is a package name. This tells Camel to go scan those packages for any classes that has been annotated with the @Converter.

...

The file in the JAR: META-INF/services/org/apache/camel/TypeConverter contains the following line(s) for FQN class names

...

...

As you can see each line in the file now contains a FQN class name. This is the recommended approach.

...

The type converter accepts the Exchange as an optional 2nd parameter. This is usable if the type converter for instance needs information from the current exchange. For instance combined with the encoding support its possible for type converters to convert with the configured encoding. An example from camel-core for the byte[] -> String converter:

...

...