How to catch deserialization error in Kafka-Spring?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Spring Kafka, a deserialization failure usually happens before your @KafkaListener method is invoked. That is why a try/catch block inside the listener does not handle the problem: the listener argument never existed because the record could not be deserialized in the first place.
Why Listener-Level try/catch Is Too Late
Consider a listener like this:
If the record value cannot be converted into OrderCreated, Spring cannot even call listen with a valid argument. The failure occurs in the consumer pipeline before your business method runs.
That means the fix belongs in consumer configuration and container error handling, not only in application logic.
Wrap the Real Deserializer With ErrorHandlingDeserializer
Spring Kafka provides ErrorHandlingDeserializer so low-level deserialization failures can be surfaced into the framework's error-handling flow.
Without that wrapper, deserialization failures are harder to route through Spring's higher-level error strategy.
Add a Container-Level Error Handler
Once Spring can surface the failure properly, configure a DefaultErrorHandler for the listener container.
This example does not retry. That is often a sensible default for malformed payloads, because a broken message rarely becomes valid on the next poll.
Dead-Letter Topics Are Often the Best Production Answer
For production systems, preserving the failed record is usually better than simply discarding it. A dead-letter topic gives you an audit trail and a place to inspect bad messages later.
That gives you a clean policy:
- malformed record arrives
- deserialization fails
- container error handler takes over
- record is published to a dead-letter topic for investigation
Separate Payload Problems From Configuration Problems
Not every deserialization error means the producer sent bad JSON. The issue could also be:
- the wrong target class
- missing trusted packages
- incompatible schema versions
- producer and consumer using different serialization formats
That is why simply saying "catch the exception" is not enough. You need to know whether the record itself is corrupt or the consumer is configured incorrectly.
Common Pitfalls
The biggest mistake is trying to catch deserialization failures inside the listener method. By then the framework has already failed earlier in the pipeline.
Another mistake is configuring DefaultErrorHandler without ErrorHandlingDeserializer. The error handler can only help after the failure is surfaced in a way the container understands.
People also forget trusted-package and target-type settings on JsonDeserializer, which can make a valid message look like a broken one.
Finally, be careful with retries. If a payload is malformed, retrying the same message over and over often just blocks the partition and creates noise.
Summary
- Spring Kafka deserialization errors usually happen before the listener method starts.
- Listener-level
try/catchis therefore not enough. - Use
ErrorHandlingDeserializerto expose failures to the container. - Pair it with
DefaultErrorHandlerto skip, retry, or dead-letter bad records. - Dead-letter topics are often the safest production strategy for malformed messages.
Related reading
- How to change default port(15672) of RabbitMQ Management plugin?
- how to change Kafka broker list ip
- How to change RabbitMQ Heartbeat without restart
- How to change the name of the topic generated by Kafka Connect Source Connector
- How to catch server error on Micronaut/Netty after client cancelled request
- How to chain non-blocking action in CompletionStage.exceptionally
- How to catch errors in synchronous functions in node.js?
- How to catch exceptions from a ThreadPool.QueueUserWorkItem?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.