Integration / Apache Camel Interview Questions
How do you use Data Formats in Camel (JSON, XML, CSV, Avro, Protobuf)?
Camel Data Formats are pluggable marshal/unmarshal strategies. You add them to a route using .marshal(dataFormat) (Java object to bytes/string) and .unmarshal(dataFormat) (bytes/string to Java object). Each Data Format is backed by a separate Maven dependency.
// JSON (Jackson):
from("direct:jsonIn").unmarshal().json(Order.class).to("direct:process");
// XML (JAXB):
JaxbDataFormat jaxb = new JaxbDataFormat("com.example.model");
from("direct:xmlIn").unmarshal(jaxb).to("direct:handleOrder");
// CSV (using OpenCSV):
from("file:/in?noop=true").unmarshal().csv().split(body()).to("direct:row");
// Avro:
AvroDataFormat avro = new AvroDataFormat(Order.SCHEMA);
from("kafka:orders?brokers=localhost:9092").unmarshal(avro).to("direct:handleAvro");
// Protobuf:
ProtobufDataFormat proto = new ProtobufDataFormat(OrderProto.OrderMessage.getDefaultInstance());
from("direct:protoIn").unmarshal(proto).to("direct:handleProto");Data Formats are registered by name in the CamelContext. You can also reference them by name in the Java DSL: .unmarshal("json"). In Spring Boot, Data Formats are auto-configured from classpath starters. Avro and Protobuf are used heavily in Kafka-based pipelines for binary efficiency; Jackson JSON is the default for REST integrations.
More Related questions...