Interface TypeConverter
- All Known Subinterfaces:
BulkTypeConverters
public interface TypeConverter
Pluggable strategy for converting objects to different
types such as String, InputStream/OutputStream, Reader/Writer, Document, byte[], and ByteBuffer.
Type converters are a central part of Camel's integration infrastructure. Whenever Camel reads an
Exchange
body or header and needs it in a different Java type, it delegates to the
TypeConverterRegistry to find an appropriate TypeConverter. Converters are
discovered automatically at startup by scanning classpath resources for the Converter annotation, or they can
be registered programmatically via
TypeConverterRegistry.addTypeConverter(Class, Class, TypeConverter).
There are three conversion variants: convertTo(Class, Object) (returns null when the conversion is unavailable),
mandatoryConvertTo(Class, Object) (throws NoTypeConversionAvailableException when no converter is found), and
tryConvertTo(Class, Object) (silently returns null without propagating exceptions).- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ObjectSentinel value indicating that no type converter was found, used internally for caching purposes. -
Method Summary
Modifier and TypeMethodDescriptionbooleanWhether the type converter allows returning null as a valid response.<T> @Nullable TConverts the value to the specified type<T> @Nullable TConverts the value to the specified type in the context of an exchange<T> TmandatoryConvertTo(Class<T> type, @Nullable Object value) Converts the value to the specified type<T> TmandatoryConvertTo(Class<T> type, @Nullable Exchange exchange, @Nullable Object value) Converts the value to the specified type in the context of an exchange<T> @Nullable TtryConvertTo(Class<T> type, @Nullable Object value) Tries to convert the value to the specified type, returning null if not possible to convert.<T> @Nullable TtryConvertTo(Class<T> type, @Nullable Exchange exchange, @Nullable Object value) Tries to convert the value to the specified type in the context of an exchange, returning null if not possible to convert.
-
Field Details
-
MISS_VALUE
Sentinel value indicating that no type converter was found, used internally for caching purposes.
-
-
Method Details
-
allowNull
boolean allowNull()Whether the type converter allows returning null as a valid response. By default null is not a valid response, returning false from this method. -
convertTo
Converts the value to the specified type- Parameters:
type- the requested typevalue- the value to be converted- Returns:
- the converted value, or null if not possible to convert
- Throws:
TypeConversionException- is thrown if error during type conversion
-
convertTo
<T> @Nullable T convertTo(Class<T> type, @Nullable Exchange exchange, @Nullable Object value) throws TypeConversionException Converts the value to the specified type in the context of an exchange Used when conversion requires extra information from the current exchange (such as encoding).- Parameters:
type- the requested typeexchange- the current exchangevalue- the value to be converted- Returns:
- the converted value, or null if not possible to convert
- Throws:
TypeConversionException- is thrown if error during type conversion
-
mandatoryConvertTo
<T> T mandatoryConvertTo(Class<T> type, @Nullable Object value) throws TypeConversionException, NoTypeConversionAvailableException Converts the value to the specified type- Parameters:
type- the requested typevalue- the value to be converted- Returns:
- the converted value, is never null
- Throws:
TypeConversionException- is thrown if error during type conversionNoTypeConversionAvailableException- if no type converters exists to convert to the given type
-
mandatoryConvertTo
<T> T mandatoryConvertTo(Class<T> type, @Nullable Exchange exchange, @Nullable Object value) throws TypeConversionException, NoTypeConversionAvailableException Converts the value to the specified type in the context of an exchange Used when conversion requires extra information from the current exchange (such as encoding).- Parameters:
type- the requested typeexchange- the current exchangevalue- the value to be converted- Returns:
- the converted value, is never null
- Throws:
TypeConversionException- is thrown if error during type conversionNoTypeConversionAvailableException- if no type converters exists to convert to the given type
-
tryConvertTo
Tries to convert the value to the specified type, returning null if not possible to convert. This method will not throw an exception if an exception occurred during conversion.- Parameters:
type- the requested typevalue- the value to be converted- Returns:
- the converted value, or null if not possible to convert
-
tryConvertTo
Tries to convert the value to the specified type in the context of an exchange, returning null if not possible to convert. This method will not throw an exception if an exception occurred during conversion. Converts the value to the specified type in the context of an exchange Used when conversion requires extra information from the current exchange (such as encoding).- Parameters:
type- the requested typeexchange- the current exchangevalue- the value to be converted- Returns:
- the converted value, or null if not possible to convert
-