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.
40.3. Built-In Type Converters
Overview Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
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.getBody(Class<T> type) or 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 String as shown in Example 40.4, “Converting a Value to a String”.
Example 40.4. Converting a Value to a String
org.apache.camel.TypeConverter tc = exchange.getContext().getTypeConverter(); String str_value = tc.convertTo(String.class, value);
org.apache.camel.TypeConverter tc = exchange.getContext().getTypeConverter();
String str_value = tc.convertTo(String.class, value);
Basic type converters Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Apache Camel provides built-in type converters that perform conversions to and from the following basic types:
java.io.FileStringbyte[]andjava.nio.ByteBufferjava.io.InputStreamandjava.io.OutputStreamjava.io.Readerandjava.io.Writerjava.io.BufferedReaderandjava.io.BufferedWriterjava.io.StringReader
However, not all of these types are inter-convertible. The built-in converter is mainly focused on providing conversions from the
File and String types. The File type can be converted to any of the preceding types, except Reader, Writer, and StringReader. The String type can be converted to File, byte[], ByteBuffer, InputStream, or StringReader. The conversion from String to File works by interpreting the string as a file name. The trio of String, byte[], and ByteBuffer are completely inter-convertible.
Note
You can explicitly specify which character encoding to use for conversion from
byte[] to String and from String to byte[] by setting the Exchange.CHARSET_NAME exchange 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 java.nio.charset.Charset class.
Collection type converters Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Apache Camel provides built-in type converters that perform conversions to and from the following collection types:
Object[]java.util.Setjava.util.List
All permutations of conversions between the preceding collection types are supported.
Map type converters Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Apache Camel provides built-in type converters that perform conversions to and from the following map types:
java.util.Mapjava.util.HashMapjava.util.Hashtablejava.util.Properties
The preceding map types can also be converted into a set, of
java.util.Set type, where the set elements are of the MapEntry<K,V> type.
DOM type converters Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
You can perform type conversions to the following Document Object Model (DOM) types:
org.w3c.dom.Document—convertible frombyte[],String,java.io.File, andjava.io.InputStream.org.w3c.dom.Nodejavax.xml.transform.dom.DOMSource—convertible fromString.javax.xml.transform.Source—convertible frombyte[]andString.
All permutations of conversions between the preceding DOM types are supported.
SAX type converters Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
You can also perform conversions to the
javax.xml.transform.sax.SAXSource type, which supports the SAX event-driven XML parser (see the SAX Web site for details). You can convert to SAXSource from the following types:
StringInputStreamSourceStreamSourceDOMSource
Custom type converters Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Apache Camel also enables you to implement your own custom type converters. For details on how to implement a custom type converter, see Chapter 42, Type Converters.