Spring Kafka JsonDesirialization MessageConversionException failed to resolve class name Class not found
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with Kafka in a Spring application, one common task is to serialize and deserialize messages to and from JSON format. The JsonDeserializer in Spring Kafka is designed to handle this task efficiently. However, issues can arise during the deserialization process, particularly around resolving class names and locating the corresponding classes. One such problem is the MessageConversionException, which can occur when the deserializer fails to find a class specified in the incoming message.
Understanding the Problem
The error typically manifests as:
This error indicates that the JsonDeserializer was unable to find the class necessary to deserialize the JSON data into a Java object. This class name resolution issue usually happens because of one of the following reasons:
- Class Not in Classpath: The class is genuinely missing from the application's classpath.
- Incorrect Configuration: Misconfiguration in the Kafka consumer configuration, especially the
value.deserializerproperty. - Incorrect JSON Data: The JSON data might contain a class type hint that does not match any class available in the application.
Example Scenario
Consider a Spring Kafka application configured to consume messages that should be deserialized into MyApp.model.Product objects. If the Maven or Gradle project dependencies do not include MyApp, or if the class name in JSON is misspelled, like MyApp.model.Produt, the deserializer will throw a MessageConversionException.
Consumer Configuration in Spring Kafka
Here's a typical Spring Kafka consumer configuration snippet:
Resolving the Issue
To address these issues, verify the following:
- Class Availability: Ensure that all classes are available in the classpath.
- Configuration Settings: Verify that
JsonDeserializeris set up correctly with the correct target class and trust package settings (JsonDeserializer.TRUSTED_PACKAGES). - JSON Data: Check the correctness of the JSON payload, focusing on the class-name fields if using type mappings.
Table: Common Causes and Solutions for Class Not Found in JsonDeserializer
| Problem | Symptoms | Possible Causes | Solutions |
| Class not found during deserialization | Error during deserialization process, application fails to process messages | Missing class in classpath, Incorrect class name in JSON, Configuration issues | Check classpath for class availability, Ensure JSON data correctness, Verify deserializer configuration |
| Incorrect package trust settings | Correct class but still failing | Security settings in JsonDeserializer | Set JsonDeserializer.TRUSTED_PACKAGES to a more inclusive setting like * or specific packages |
Additional Considerations
- Performance Implications: Incorrectly configuring the trusted packages (e.g., using
*) can cause performance issues and potentially expose the application to deserialization attacks. Always use the most restrictive setting that fits your setup. - Library Versions: Compatibility issues between different versions of Spring Kafka and the underlying Kafka client library can also lead to deserialization issues. Ensure all dependencies are compatible.
Troubleshooting issues with deserializing JSON messages in a Kafka-Spring integration often boils down to a careful examination of configuration settings, classpath contents, and the correctness of the involved JSON messages. By meticulously checking each element, developers can ensure smooth and error-free message processing in their applications.
Related reading
- Spring Kafka JsonSerializer usage
- Spring kafka @KafkaListener is not being invoked
- Spring Kafka KafkaTemplate.flush() required?
- Spring Kafka listener infinite loop on error
- spring kafka listener is looking ContainerProperties at wrong place
- Spring Kafka MessageListenerContainer
- Spring Kafka, overriding max.poll.interval.ms?
- Spring Kafka Producer not sending to Kafka 1.0.0 (Magic v1 does not support record headers)

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.