JsonSerializer and JsonDeserializer are Spring for Apache Kafka classes (package org.springframework.kafka.support.serializer) that convert Java objects to JSON bytes on the producer side and back into objects on the consumer side, using Jackson. Apache Kafka itself ships no JSON serializer, only primitives, strings and byte arrays, so these classes are what let you send typed objects such as an OrderCreated event without writing the conversion by hand.
Kafka brokers only store bytes. The producer's value.serializer and the consumer's value.deserializer decide how an object becomes bytes and back, which is why a mismatch between the two sides is one of the most common causes of consumer failures.
A producer calls serialize(topic, data) on the configured serializer before the record is sent. A consumer calls deserialize(topic, bytes) on every record it polls. Both Spring classes delegate the actual JSON work to a Jackson ObjectMapper.
| Concern | Plain Kafka client | Spring Kafka JSON classes |
|---|---|---|
| Strings, numbers, bytes | StringSerializer, LongSerializer, ByteArraySerializer | Not needed |
| Java objects as JSON | Write your own Serializer | JsonSerializer, JsonDeserializer |
| Schema enforcement | Not included | Not included (use Schema Registry for that) |
| Type information | None | Optional __TypeId__ header |
By default JsonSerializer adds a __TypeId__ header holding the class name, and JsonDeserializer uses it to decide which class to build. That is convenient inside one codebase and a trap across services, as the sections below explain.
With Spring Boot you configure the serializers in application.yml and inject the auto-configured KafkaTemplate:
The record is written to the orders topic as a JSON document such as {"orderId":"A-100","customer":"Alice","quantity":2}. Using the order id as the key keeps all events of one order in the same partition, which preserves their order.
Without Spring Boot, you can build the producer yourself. Create a DefaultKafkaProducerFactory with the same ProducerConfig keys, or pass a configured JsonSerializer instance to its constructor.
The consumer needs the matching deserializer and a list of packages it is allowed to instantiate:
Two settings matter here:
spring.json.trusted.packages is a security control. Deserializing arbitrary class names from a header would let anyone who can write to the topic make your service instantiate classes of their choosing, so only listed packages are accepted. Avoid * outside local experiments.spring.json.value.default.type tells the deserializer which class to use when no usable type header exists, which is the normal situation when the producer is not a Spring application.When the producer and consumer live in different services, the producer's class name (for example com.shop.orders.OrderCreated) does not exist on the consumer's classpath. There are two clean ways to handle this:
spring.json.add.type.headers: false and set spring.json.value.default.type on the consumer.spring.json.type.mapping, for example order:com.shop.orders.OrderCreated on the producer and order:com.example.events.OrderCreated on the consumer.Option 2 gives you several event types on one topic, because the consumer can still pick the class per record. Unknown JSON fields are another cross-team concern. The default mapper used by these classes is configured not to fail on unknown properties, so a producer can add fields without breaking older consumers. If you pass in your own ObjectMapper, check that setting.
A single malformed record (a "poison pill") makes JsonDeserializer throw inside the consumer's poll, and the consumer then retries the same offset forever. Wrap the deserializer with ErrorHandlingDeserializer so the failure is captured and passed to the container's error handler instead:
Combine this with a DefaultErrorHandler and a DeadLetterPublishingRecoverer to move undecodable records to a dead-letter topic.
Spring Kafka 4.0 moves to Jackson 3 (with Spring Boot 4). In that line, Jackson 3 based classes named JacksonJsonSerializer and JacksonJsonDeserializer take over the role of the classes described here, and the older names are deprecated. The configuration properties and concepts stay the same. Check the Spring Kafka reference for the version you use before copying class names.
No. The Kafka clients library provides serializers for strings, numbers, UUIDs and byte arrays. The JSON classes come from Spring Kafka, or from third-party libraries such as Confluent's serializers.
The __TypeId__ header names a class whose package is not listed in spring.json.trusted.packages. Add the package of your event classes, or ignore the header by setting a default type and disabling type headers on the producer.
JSON is simple and readable. Avro or Protobuf with a schema registry give you enforced schemas, compatibility checks and smaller messages, which matters when many teams share topics.
Use type mapping with spring.json.type.mapping, or a DelegatingByTypeSerializer on the producer side and a typed @KafkaListener class-level handler with @KafkaHandler methods on the consumer side.
JsonSerializer and JsonDeserializer are Spring Kafka's Jackson-based bridge between Java objects and the JSON bytes stored in Kafka. Configure trusted packages, decide deliberately between type headers and a default type, wrap the deserializer in ErrorHandlingDeserializer, and consider a schema registry once several teams share your topics.