The MessageConverter interface in Spring for Apache Kafka converts between Kafka's ConsumerRecord and ProducerRecord objects and the Spring Message or domain objects your code works with. It sits between the listener container and your @KafkaListener method, so the method can take a typed payload such as OrderEvent instead of raw bytes or strings.
It lives in org.springframework.kafka.support.converter. The two practical sub-interfaces are RecordMessageConverter for single records and BatchMessageConverter for batch listeners.
A Kafka client deserialises keys and values with a Deserializer, which produces the values inside a ConsumerRecord. The message converter then runs one step later: it takes that record and builds the argument for your listener method, including headers such as the topic, partition, offset and key.
| Layer | Job | Typical class |
|---|---|---|
| Kafka client | bytes to key and value objects | StringDeserializer, JsonDeserializer |
| Message converter | ConsumerRecord to listener argument | StringJsonMessageConverter |
You can do the conversion in either layer. Using a JsonDeserializer in the consumer factory is the more common choice in current Spring Boot applications. A message converter is useful when the record holds a String or byte[] and the payload type should be decided by the listener method signature.
Spring Kafka ships several implementations:
MessagingMessageConverter: the default. It maps the record to a Message and copies Kafka headers to message headers.StringJsonMessageConverter: reads a JSON String value and converts it to the type declared on the listener parameter.ByteArrayJsonMessageConverter: same idea for byte[] values.BytesJsonMessageConverter: for the Kafka Bytes type.JsonMessageConverter: the abstract base class for the three JSON variants.ProjectingMessageConverter: builds Spring Data style projection interfaces from JSON.BatchMessagingMessageConverter: the batch counterpart of MessagingMessageConverter.Set the converter on the listener container factory. The value deserializer stays a StringDeserializer, and the converter parses the JSON.
The listener declares the domain type, and the converter infers it from the method parameter:
In Spring Boot you can instead declare a RecordMessageConverter bean, and Boot's auto-configuration applies it to the default container factory.
KafkaTemplate has a matching hook, setMessagingConverter, that converts a Spring Message to a ProducerRecord. Most producers skip this and use JsonSerializer as the value serializer. For the JSON serializer and deserializer, set trusted packages deliberately with spring.json.trusted.packages, because deserialising arbitrary classes by type header is a security risk.
Implement RecordMessageConverter when you need to handle a format Spring does not cover, such as a custom envelope or Avro wrapped in your own header:
toMessage receives the target payloadType, which comes from the listener parameter, so one converter can serve many methods.
For batch listeners (batchListener = true or factory.setBatchListener(true)), set a BatchMessageConverter through setBatchMessageConverter. The built-in BatchMessagingMessageConverter can wrap a record converter, so each record in the batch is converted with the same rules.
Both work. A JsonDeserializer is configured in the consumer properties and is the usual choice. A message converter lets the listener method signature decide the target type and keeps the Kafka client on plain strings.
MessagingMessageConverter, which maps records to Spring messages and keeps headers.
Define several container factories and select one with containerFactory on @KafkaListener.
The JsonDeserializer only deserialises classes from trusted packages. Add yours with spring.json.trusted.packages.
The Spring Kafka MessageConverter turns Kafka records into the Spring messages and domain objects your listener expects. Use StringJsonMessageConverter when the listener signature should drive the payload type, a JsonDeserializer for fixed types, and implement RecordMessageConverter for custom formats.