Spring Boot / Kafka Json Deserialization - Trusted Packages
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Kafka can turn a JSON message body into a Java object automatically, but that convenience comes with a safety question: which classes should the consumer be allowed to create? The trusted.packages setting answers that question by restricting deserialization targets to specific package names instead of allowing arbitrary class resolution.
Why Trusted Packages Exist
JsonDeserializer can use type headers or configured target types to decide which Java class should receive the incoming JSON. If the consumer trusts every package, a malicious or misconfigured producer could try to steer deserialization toward an unexpected class. Spring Kafka therefore exposes a trusted-package list as a guardrail.
In practice, this means you should think about event contracts, not just message format. If your service only consumes com.example.orders.OrderCreatedEvent, there is usually no reason to trust unrelated packages.
The common property name in Spring Boot is spring.json.trusted.packages under the consumer properties block. A narrow configuration is the safest default.
Configuring Trusted Packages In Properties
For a Boot application, the simplest setup is property-based configuration:
The YAML form is the same setting expressed hierarchically:
The value.default.type line is useful when the producer does not send type headers and the consumer always expects the same payload class.
Consuming A Typed Event
Assume the topic carries a simple order event:
With the trusted package configured, the listener can receive the typed object directly:
This is the ideal outcome: the consumer uses a clear event type, and deserialization is limited to the package that owns that type.
Programmatic Configuration
If you need more control, configure the deserializer as a bean. This is useful when different consumers in the same application need different target types or trust boundaries.
One important detail from the Spring Kafka API is that you should configure the deserializer consistently. Mixing direct setter calls with a later configure call can lead to confusing results because not every combination is applied the way people expect.
When * Is Acceptable And When It Is Not
You will often see this during local debugging:
That disables the package filter and can make an early prototype work quickly. The tradeoff is that you lose an explicit safety boundary. In a production service, * is usually too broad unless the environment is tightly controlled and you fully understand the risk.
If your application consumes only a small set of event types, trusting the specific package names is both safer and easier to reason about during incident response.
Common Pitfalls
- Using
*in production because it makes deserialization errors disappear without fixing the underlying contract problem. - Trusting the wrong package after moving model classes into another module or namespace.
- Forgetting that missing or mismatched type headers can still break deserialization even when the trusted package list is correct.
- Setting a typed listener method while the producer sends a payload for a different class shape.
- Mixing property-based and programmatic deserializer configuration without understanding which values take effect.
Summary
- Trusted packages limit which Java packages Spring Kafka may use during JSON deserialization.
- Narrow package lists are the right default for production consumers.
- '
spring.json.value.default.typehelps when type headers are absent and only one event type is expected.' - '
*is convenient for local experiments but weakens the deserialization boundary.' - Correct deserialization still depends on aligned payloads, headers, and event classes.
Related reading
- Spring Boot & Kafka, Producer thrown exception with key=''null''
- Spring Boot auto configuration of Kafka Producers with multiple De-Serializer types
- Spring Boot containers can not connect to the Kafka container
- Spring boot Kafka class deserialization - not in the trusted package
- Spring Boot access static resources missing scr/main/resources
- Spring Boot Actuator - LDAP Health Endpoint Throwing NPE
- Spring Boot Kafka Commit cannot be completed since the group has already rebalanced
- Spring Boot Kafka Configure DefaultErrorHandler?

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.