이 콘텐츠는 선택한 언어로 제공되지 않습니다.

40.3. Built-In Type Converters


Overview

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);
Copy to Clipboard Toggle word wrap

Basic type converters

Apache Camel provides built-in type converters that perform conversions to and from the following basic types:
  • java.io.File
  • String
  • byte[] and java.nio.ByteBuffer
  • java.io.InputStream and java.io.OutputStream
  • java.io.Reader and java.io.Writer
  • java.io.BufferedReader and java.io.BufferedWriter
  • java.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

Apache Camel provides built-in type converters that perform conversions to and from the following collection types:
  • Object[]
  • java.util.Set
  • java.util.List
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:
  • java.util.Map
  • java.util.HashMap
  • java.util.Hashtable
  • java.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

You can perform type conversions to the following Document Object Model (DOM) types:
  • org.w3c.dom.Document—convertible from byte[], String, java.io.File, and java.io.InputStream.
  • org.w3c.dom.Node
  • javax.xml.transform.dom.DOMSource—convertible from String.
  • javax.xml.transform.Source—convertible from byte[] and String.
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.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:
  • String
  • InputStream
  • Source
  • StreamSource
  • DOMSource

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 42, Type Converters.
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat