Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A producer team moved its event class from com.acme.orders.events.OrderPlaced to com.acme.orders.api.OrderPlaced. Since their release, a consumer in another team fails with a class-not-found or "not in the trusted packages" error for every record. Explain the cause and how to decouple producers and consumers.

The producer's JSON serializer writes its fully qualified class name into the __TypeId__ header, so after the move every record says com.acme.orders.api.OrderPlaced. The consumer tries to create that class, which it either does not have or does not trust, and fails on every record. The quick fix is on the consumer: ignore type headers and use a default type, or map the new name to the local class. The lasting fix is to stop putting Java class names on the wire, using logical type names, one event type per topic or a schema registry, and to treat the event as a versioned contract.

Why this question is asked

This incident is a classic example of accidental coupling. Nobody changed the JSON, the fields are identical, and yet a consumer owned by another team breaks the moment the producer refactors a package. Interviewers ask about it because the fix shows whether we understand what really travels inside a Kafka record and whether we think of events as contracts between teams. In this answer, we will recap how Spring's JSON serializer records the type, reproduce the failure, apply the short-term consumer fix, then design the decoupled version with logical type names or schemas, and finally discuss why a shared event jar invites this problem.

1A Quick Recap of Type Headers

Spring Kafka's JSON serializer does two things with each value. It writes the object as JSON with Jackson, and by default it adds a header named __TypeId__ containing the Java class name of the object. The consumer's JSON deserializer reads the header and asks Jackson to create an instance of that class from the JSON. For safety, it only creates classes from packages listed in spring.json.trusted.packages.

So the header ties the consumer to a detail of the producer's code: the exact package and class name. As long as both sides share the same class in the same package, nobody notices.

2Reproducing the Failure

Before the release the records carried the old name, and the consumer had a matching class and trusted its package. After the release the JSON is the same but the header changed.

Java
// Before the producer release
// headers: __TypeId__ = com.acme.orders.events.OrderPlaced
// value: {"orderId":"o-42","amountCents":12500}
// After the producer release
// headers: __TypeId__ = com.acme.orders.api.OrderPlaced
// value: {"orderId":"o-42","amountCents":12500} // unchanged
// Consumer configuration
// spring.json.trusted.packages = com.acme.orders.events
// -> "The class 'com.acme.orders.api.OrderPlaced' is not in the trusted packages"
// or, if trusted is "*": ClassNotFoundException for com.acme.orders.api.OrderPlaced
What changed on the wire
Part of the recordBeforeAfter
Keyo-42o-42
JSON valuesame fieldssame fields
__TypeId__ header...orders.events.OrderPlaced...orders.api.OrderPlaced
Consumer resultOrderPlaced creatednot trusted / class not found
Only the header changed, and the header is what the consumer depends on.

Which error we see depends on the trusted packages setting, but both mean the same thing: the consumer was told to build a class it does not know. Since the failure happens in deserialization, every record fails, and if the consumer does not use ErrorHandlingDeserializer the partition is stuck on the first new record.

3The Short-Term Fix on the Consumer

The consumer team needs to recover now, without waiting for the producer. The simplest fix is to tell the deserializer to ignore the type header and always create the consumer's own class. That works when the topic carries one event type.

YAML
spring:
kafka:
consumer:
value-deserializer: org.springframework.kafka.support.serializer.ErrorHandlingDeserializer
properties:
spring.deserializer.value.delegate.class: org.springframework.kafka.support.serializer.JsonDeserializer
spring.json.use.type.headers: false # ignore __TypeId__
spring.json.value.default.type: com.acme.billing.events.OrderPlaced # our own class

Notice that the consumer's class does not even need to live in the producer's package any more. It only needs fields that match the JSON. If the topic carries several types, ignoring the header is not enough, and we would map type ids to local classes instead, which is the next step. In Spring Kafka 4 the JSON classes have Jackson 3 based names, so check your version for the class names, while the property names stay the same idea.

4The Better Fix: Logical Type Names

Type mappings replace the class name in the header with a short logical name that both sides agree on. The producer maps its class to the token, and the consumer maps the same token to its own class.

Properties
# Producer (orders team)
spring.kafka.producer.properties.spring.json.type.mapping=\
orderPlaced:com.acme.orders.api.OrderPlaced,orderCancelled:com.acme.orders.api.OrderCancelled
# Consumer (billing team)
spring.kafka.consumer.properties.spring.json.type.mapping=\
orderPlaced:com.acme.billing.events.OrderPlaced,orderCancelled:com.acme.billing.events.OrderCancelled
# On the wire: __TypeId__ = orderPlaced (no package, no class name)

Now the producer can move or rename its class freely, because only its own mapping line changes and the header still says orderPlaced. The consumer controls which local class receives it. The token becomes part of the contract, so it is chosen deliberately and never reused for a different meaning.

5Stopping Java Types From Leaking Onto the Wire

We can go one step further and send no Java type information at all. The cleanest arrangement is one event type per topic, so the consumer knows the type from the topic and needs no header. When a topic must carry several related types, a type field inside the payload, such as "eventType": "OrderPlaced", with a version field next to it, keeps the information in the data where any language can read it.

For events shared across many teams, a schema registry with Avro, Protobuf or JSON Schema adds the missing safety: every schema version is registered, compatibility rules such as backward compatibility are checked before the producer can publish, and a breaking change is rejected at build or deploy time instead of in the consumer's production logs. Whatever the mechanism, the principle is the same: the event is a published contract with versions and owners, not shared code that can be refactored at will.

6Follow-up questions

What are the risks of sharing an event jar between producer and consumer?

A shared jar makes the event look like ordinary code, so it gets refactored like ordinary code: packages move, fields are renamed, and Java type names end up on the wire, exactly as in this incident. It also couples release schedules, because a consumer must upgrade the jar to read new events, and it pulls the producer's dependencies, such as a Jackson version or validation annotations, into every consumer. Consumers in other languages cannot use it at all. A schema, or at least a documented JSON format with logical type names, gives the same safety without binding teams to one artifact.

Could the consumer simply trust all packages to fix it?

No. Trusting everything removes a security protection, and it still fails with ClassNotFoundException because the consumer has no class in the new package. The fix is to stop following the producer's class name, not to follow it more freely.

How would you roll out type mappings without downtime?

First add the mapping on the consumer, including the old class name mapped to the local class if needed, and deploy it, because a consumer that understands both forms is safe. Then change the producer to send the logical name. Records in the topic with old headers keep working, and new records use the token.

7Conclusion

In this answer, we saw that the consumer broke because Spring's JSON serializer put the producer's fully qualified class name in the __TypeId__ header, and the consumer tried to build a class it neither had nor trusted. The quick fix is consumer-side: ignore the type header with a default type, or map ids to local classes. The lasting fix is to keep Java class names off the wire with logical type mappings, one event type per topic, a type field in the payload, or a schema registry with compatibility checks, and to manage events as versioned contracts.

What to say in the interviewAbout 80 seconds aloud

Open with

The JSON itself did not change; the header did. Spring Kafka's JSON serializer adds a type id header containing the producer's fully qualified class name, so after the refactoring every record says com.acme.orders.api.OrderPlaced.

Then, if they want more

The consumer's JSON deserializer uses that header to decide what class to instantiate, and it either does not have that class or the package is not in its trusted packages, so every record fails in deserialization.

To recover quickly on the consumer side, I would turn off use of type headers and set a default value type to our own OrderPlaced class, which works if the topic has one event type; with several types I would map type ids to local classes. The real fix is to stop sending Java class names.

Both sides configure spring.json.type.mapping with logical names like orderPlaced, so the producer can move its classes without changing the wire format, or we use one event type per topic or a type field in the payload, ideally with a schema registry that checks compatibility before a producer can publish.

The broader lesson is that events are a contract between teams, versioned and owned, and not shared code that can be refactored freely; a shared event jar tends to cause exactly this kind of break.

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.
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 "A producer team moved its event class from com.acme.orders.events.OrderPlaced to...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/060352053c/