The Serializer and Deserializer interfaces in the Kafka client library convert your Java objects to byte arrays before a producer sends them and convert the bytes back into objects when a consumer reads them. Kafka brokers only store bytes, so every record key and value passes through one of each pair. Spring Boot and Spring Kafka plug these classes in through configuration.
Both interfaces live in org.apache.kafka.common.serialization. Kafka ships ready-made implementations for strings, numbers and byte arrays, and Spring Kafka adds JSON support on top.
A Serializer<T> turns a value of type T into byte[]. The producer calls it for the key and for the value of each record, using the classes set in key.serializer and value.serializer.
Only serialize(String, T) is mandatory. configure and close have default implementations, and the overload with Headers lets you write metadata such as a schema id or content type into the record headers.
Built-in serializers include StringSerializer, IntegerSerializer, LongSerializer, DoubleSerializer, UUIDSerializer and ByteArraySerializer.
A Deserializer<T> is the reverse: it receives the topic name and the raw bytes and returns a T.
Kafka passes null for tombstone records (a key with a null value), so your implementation must return null for a null input instead of throwing.
The example uses Jackson and a Java record. Data classes should be records or simple immutable types when you can.
Throw SerializationException so the Kafka client and Spring error handling recognise the failure. The mapper is created once per instance, because ObjectMapper is thread-safe after configuration and expensive to create.
Register the classes in application.yml:
Then use KafkaTemplate and @KafkaListener as usual:
For plain JSON you rarely need to write your own. Spring Kafka provides JsonSerializer and JsonDeserializer in org.springframework.kafka.support.serializer. The deserializer only creates classes from trusted packages, so set that property:
By default JsonSerializer adds a type header to each record. Consumers in other applications may not have the same class name, so set spring.json.use.type.headers=false and spring.json.value.default.type when you share a topic across services.
A deserializer that throws inside the consumer poll loop makes the consumer fail on the same offset forever (a "poison pill"). Wrap it with ErrorHandlingDeserializer:
The wrapper catches the exception and hands it to the container error handler, which can retry or send the record to a dead-letter topic. For schemas that change over time, use Avro, Protobuf or JSON Schema with a schema registry serializer instead of hand-written JSON, so producers and consumers agree on compatibility rules.
No. Use JsonSerializer or a schema registry serializer for most objects. Write your own only for special formats or when you need full control of the bytes.
Yes. They are configured separately. A common pair is StringSerializer for the key and a JSON or Avro serializer for the value.
It is the first argument of serialize and deserialize. Registry-based serializers use it to derive the subject name.
The consumer fails with a SerializationException, or worse, reads wrong data. Keep the pair in a shared module and version your events.
Serializer converts objects to bytes for the producer, and Deserializer converts bytes back to objects for the consumer. Configure them per key and value, return null for null, throw SerializationException on errors, and prefer Spring Kafka's JSON classes or a schema registry over custom code. Wrap deserializers with ErrorHandlingDeserializer so one bad record cannot stall a partition.