Integration / Apache Camel Interview Questions
What data transformation options does Camel provide (Type Converters, Data Formats, Transformers)?
Apache Camel provides three complementary layers for data transformation:
- Type Converters: Implicit, automatic conversions between Java types (e.g., byte[] to String, File to InputStream). Applied transparently when you call getBody(TargetClass.class). Registered via @Converter annotations or explicit TypeConverterRegistry.
- Data Formats: Marshal (Java object —> bytes/String) and unmarshal (bytes/String —> Java object) operations. Used explicitly in routes with .marshal() and .unmarshal(). Formats include JSON (Jackson), XML (JAXB), CSV, Avro, Protobuf, EDI, HL7.
- Transformers: Route-level pipeline steps that use a processor, bean, or expression to reshape the body. This includes .transform(expression), .setBody(), .enrich(), and custom Processors.
// Type converter (implicit, no code needed in route):
String body = exchange.getIn().getBody(String.class); // auto-converts byte[]
// Data format: JSON marshal/unmarshal
from("direct:in")
.unmarshal().json(Order.class) // JSON bytes -> Order POJO
.process(new OrderProcessor())
.marshal().json() // Order POJO -> JSON bytes
.to("direct:out");
// Transformer using expression:
from("direct:transform")
.transform(simple("${body.toUpperCase()}"));Type Converters are always implicit — they run without any route configuration. Data Formats require explicit marshal()/unmarshal() calls and add the conversion to the pipeline. Transformers are explicit processing steps. Choosing between them: if you need structural format change (JSON to XML), use Data Formats; if you need business-level object manipulation, use Processors or Beans; implicit type coercion uses converters.
More Related questions...