Quick revision
The short answer
For JSON values we set the producer's value serializer to Spring Kafka's JSON serializer, which writes the object with Jackson and by default adds a __TypeId__ header holding the class name. The consumer's JSON deserializer uses that header, or a configured default type, to choose the class to create, and spring.json.trusted.packages limits which classes it may create, so a header cannot make it build arbitrary types. We wrap the deserializer in ErrorHandlingDeserializer so one bad record becomes a DeserializationException for the error handler, and can go to a dead-letter topic, instead of failing every poll.
What to remember
In this answer, we configured Spring Kafka JSON on both sides, with string keys and JSON values, and saw that the serializer adds a __TypeId__ header with the producer's class name, which the deserializer uses unless a default type is set. Trusted packages stop a header from choosing arbitrary classes. ErrorHandlingDeserializer wraps the real deserializer so a malformed record becomes a DeserializationException that the error handler can send to a dead-letter topic, instead of a poll that fails forever. For topics shared across many teams, a schema-based format with a registry is usually the better contract, and in Spring Kafka 4 the JSON class names change with Jackson 3.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For JSON values we set the producer's value serializer to Spring Kafka's JSON serializer, which writes the object with Jackson and by default adds a __TypeId__ header holding the class name. The consumer's JSON deserializer uses that header, or a configured default type, to choose the class to create, and spring.json.trusted.packages limits which classes it may create, so a header cannot make it build arbitrary types. We wrap the deserializer in ErrorHandlingDeserializer so one bad record becomes a DeserializationException for the error handler, and can go to a dead-letter topic, instead of failing every poll.
Why this question is asked
Almost every Spring Kafka service sends JSON, and the defaults work so smoothly that many developers never learn what is in the record besides the payload. Then a producer renames a class, or a malformed record arrives, and the consumer stops. Interviewers ask this to see whether we understand the moving parts: who writes the type information, who reads it, why the deserializer refuses some classes, and what happens when deserialization fails. In this answer, we will recap serializers and deserializers, configure JSON on both sides, look inside the __TypeId__ header, see why trusted packages exist, and then follow a bad record with and without ErrorHandlingDeserializer.
1A Quick Recap of Serializers and Deserializers
Kafka itself only stores bytes. A record has a key, a value and headers, and the broker does not care what the bytes mean. The producer client needs a serializer, an object that turns our Java value into bytes, and the consumer needs a deserializer, which turns the bytes back into an object. Keys and values have separate settings.
For keys, a plain string such as an order id is common, so StringSerializer and StringDeserializer are the usual choice. For values we often want a structured object, and JSON through Jackson is the easiest format to start with, which is why Spring Kafka ships a JSON serializer and deserializer.
2Configuring JSON on the Producer and the Consumer
With Spring Boot, we configure both sides in application.yml. The class names below are the Spring Kafka 3.x ones. Spring Kafka 4 moves to Jackson 3 and introduces Jackson-named classes such as JacksonJsonSerializer, deprecating the old ones, so check your version for the exact names.
Notice that the consumer's value deserializer is not the JSON deserializer itself. It is ErrorHandlingDeserializer, and the JSON deserializer is configured as its delegate, the object it calls to do the real work. We will see why in a moment. With this in place, kafkaTemplate.send("orders", order.id(), order) sends an OrderPlaced object, and a @KafkaListener method with an OrderPlaced parameter receives it.
3What the __TypeId__ Header Carries
By default, the JSON serializer adds a header to each record that tells the consumer what class the JSON came from. The header is called __TypeId__ and holds the fully qualified class name as bytes.
6 messages in order. Play them, or step through them one at a time.
On the consumer side, the JSON deserializer reads this header and creates an instance of that class. If type headers are switched off, or there is no header, it falls back to the default type configured with spring.json.value.default.type. The convenience is that one generic consumer can receive whatever type the producer sent. The cost is that the producer's Java class name is now part of the wire format, which matters as soon as the producer and consumer are owned by different teams.
4Why the Deserializer Has Trusted Packages
Letting a header choose which class to instantiate is risky. Anyone who can write to the topic can put any class name in __TypeId__, and Jackson would try to build that class from the JSON. Deserializing attacker-chosen types has a long history of security problems, because some classes do dangerous things when they are created or have setters called. So the JSON deserializer only creates classes from packages we list in spring.json.trusted.packages, and by default it trusts only java.util and java.lang plus the default type. A header naming any other class fails with an error saying the class is not in the trusted packages.
Setting trusted packages to "*" switches the protection off. It is tempting in examples, but in production we list our own event packages. This also catches honest mistakes early, such as a producer accidentally sending an internal entity class.
5Following a Bad Record With and Without ErrorHandlingDeserializer
Deserialization happens inside the Kafka client's poll(), before Spring sees the record. If the plain JSON deserializer meets invalid JSON, poll() throws. The container never gets the record, cannot pass it to the listener's error handler, and does not commit past it, so the next poll fails on the same record again. One bad record blocks the partition, and the logs repeat the same exception endlessly.
ErrorHandlingDeserializer solves this by catching the exception from its delegate. It returns null as the value and stores the failure in a record header. The container sees the header and raises a DeserializationException for that record, which goes to DefaultErrorHandler like any other listener failure. DefaultErrorHandler treats DeserializationException as not retryable, since retrying the same bytes cannot help, and if we configured a DeadLetterPublishingRecoverer it publishes the original bytes to the dead-letter topic, orders.DLT by default. The partition moves on.
| JsonDeserializer only | Wrapped in ErrorHandlingDeserializer | |
|---|---|---|
| Where it fails | inside poll(), before Spring | delegate fails, wrapper catches it |
| Error handler sees it | no | yes, as DeserializationException |
| Retries | the same poll fails again and again | none: not retryable |
| Partition | stuck on that offset | moves on after recovery |
| Bad bytes kept | only in the log | in orders.DLT with headers |
6When JSON Is Not Enough
JSON with Spring's type headers is fine when one team owns both sides. When many teams share topics, a schema-based format such as Avro or Protobuf with a schema registry is usually better: the schema is the contract, the registry checks that a new version stays compatible before the producer can use it, and consumers in other languages can read the data without Java class names. The choice is less about speed and more about who is allowed to change the format and how changes are checked.
7Follow-up questions
How would you send two different event types on one topic?
With JSON, the simplest way is to keep type information but make it logical: configure spring.json.type.mapping on both sides, for example orderPlaced:com.acme.events.OrderPlaced and orderCancelled:com.acme.events.OrderCancelled, so the header carries a short name instead of a class. On the consumer, a class-level @KafkaListener with one @KafkaHandler method per type lets Spring call the right method. Another approach is a common envelope with a type field in the JSON and Jackson polymorphic handling. With a schema registry, Avro or Protobuf unions or record-name subject strategies do the same job with checked schemas.
Why not set spring.json.trusted.packages to star?
Because it lets anyone who can produce to the topic choose which class the consumer instantiates, which removes the protection against unsafe deserialization. It also hides mistakes such as a producer sending an internal class. Listing our own event packages costs one line.
What does the value look like in the listener when deserialization failed?
The listener is normally not called at all for that record; the container raises a DeserializationException to the error handler. If we write our own handling, the original bytes and the exception are available from the record headers that ErrorHandlingDeserializer added.
8Conclusion
In this answer, we configured Spring Kafka JSON on both sides, with string keys and JSON values, and saw that the serializer adds a __TypeId__ header with the producer's class name, which the deserializer uses unless a default type is set. Trusted packages stop a header from choosing arbitrary classes. ErrorHandlingDeserializer wraps the real deserializer so a malformed record becomes a DeserializationException that the error handler can send to a dead-letter topic, instead of a poll that fails forever. For topics shared across many teams, a schema-based format with a registry is usually the better contract, and in Spring Kafka 4 the JSON class names change with Jackson 3.
Open with
On the producer I set the key serializer to StringSerializer and the value serializer to Spring Kafka's JSON serializer, which uses Jackson to write the object. By default it also adds a header called underscore underscore TypeId with the fully qualified class name of the object.
Then, if they want more
On the consumer, the JSON deserializer reads that header to decide which class to create, or falls back to a default type if I configure one or turn type headers off.
Because a header could name any class, the deserializer only instantiates classes from packages listed in spring.json.trusted.packages; that is a security measure against unsafe deserialization, so I list our event packages and never use star in production. I always wrap the JSON deserializer in ErrorHandlingDeserializer.
Without it, a malformed record fails inside poll, before Spring can handle it, so the container retries the same offset forever and the partition is stuck. With the wrapper, the failure becomes a DeserializationException passed to DefaultErrorHandler, which does not retry it and can publish the raw bytes to a dead-letter topic.
For many teams sharing topics I would move to Avro or Protobuf with a schema registry. And in Spring Kafka 4 the classes move to Jackson 3 names, so I would check the version.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In Spring Kafka, explain how you configure JSON serialization for values, what the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/33af1bbd82/