Show Table of Contents
43.3. Built-In Type Converters
This section describes the conversions supported by the master type converter. These conversions are built into the Apache Camel core.
Usually, the type converter is called through convenience functions, such as
Message.getHeader(String name, Class<T> type). It is also possible to invoke the master type converter directly. For example, if you have an exchange object,
exchange, you could convert a given value to a
Stringas shown in Example 43.4, “Converting a Value to a String”.
Example 43.4. Converting a Value to a String
org.apache.camel.TypeConverter tc = exchange.getContext().getTypeConverter(); String str_value = tc.convertTo(String.class, value);
Basic type converters
Apache Camel provides built-in type converters that perform conversions to and from the following basic types:
However, not all of these types are inter-convertible. The built-in converter is mainly focused on providing conversions from the
Filetype can be converted to any of the preceding types, except
Stringtype can be converted to
StringReader. The conversion from
Fileworks by interpreting the string as a file name. The trio of
ByteBufferare completely inter-convertible.
You can explicitly specify which character encoding to use for conversion from
byteby setting the
Exchange.CHARSET_NAMEexchange property in the current exchange. For example, to perform conversions using the UTF-8 character encoding, call
exchange.setProperty("Exchange.CHARSET_NAME", "UTF-8"). The supported character sets are described in the
Collection type converters
Apache Camel provides built-in type converters that perform conversions to and from the following collection types:
All permutations of conversions between the preceding collection types are supported.
Map type converters
Apache Camel provides built-in type converters that perform conversions to and from the following map types:
The preceding map types can also be converted into a set, of
java.util.Settype, where the set elements are of the
DOM type converters
You can perform type conversions to the following Document Object Model (DOM) types:
All permutations of conversions between the preceding DOM types are supported.
SAX type converters
You can also perform conversions to the
javax.xml.transform.sax.SAXSourcetype, which supports the SAX event-driven XML parser (see the SAX Web site for details). You can convert to
SAXSourcefrom the following types:
enum type converter
Camel provides a type converter for performing
enumtype conversions, where the string value is converted to the matching
enumconstant from the specified enumeration class (the matching is case-insensitive). This type converter is rarely needed for converting message bodies, but it is frequently used internally by Apache Camel to select particular options.
For example, when setting the logging level option, the following value,
INFO, is converted into an
enumtype converter is case-insensitive, any of the following alternatives would also work:
<to uri="log:foo?level=info"/> <to uri="log:foo?level=INfo"/> <to uri="log:foo?level=InFo"/>
Custom type converters
Apache Camel also enables you to implement your own custom type converters. For details on how to implement a custom type converter, see Chapter 45, Type Converters.