Sub topic 5
Keys, values and JSON serialization
This chapter assumes you know Kafka topics, partitions and offsets, and that you have Spring Boot connected to a broker. You will see how a record's key and value are turned into bytes and back, and how to configure JSON for both.
Keys and values
Every Kafka record has a key and a value. The broker stores both as raw bytes. It never looks inside them. Your application must convert Java objects to bytes when producing, and bytes to Java objects when consuming.
- The value is the payload, such as an order event.
- The key decides the partition. The producer hashes the key bytes, so records with the same key always go to the same partition.
Kafka preserves order only within a partition. If you use the order ID as the key, all events for one order stay in sequence. A null key spreads records across partitions, and you lose per-entity ordering.
The payload
The examples use a record as the message payload:
package com.example.orders;
public record OrderEvent(String orderId, long amountCents) {}Configuring serializers
Spring Boot reads serializer settings from spring.kafka.*. This configuration uses a string key and a JSON value on both sides:
spring:
kafka:
bootstrap-servers: localhost:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
acks: all
properties:
enable.idempotence: true
consumer:
group-id: order-service
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
properties:
spring.json.trusted.packages: com.example.orders
spring.json.value.default.type: com.example.orders.OrderEventThe producer side is simple. JsonSerializer uses Jackson to write the object as JSON bytes. By default it also adds a type header to each record that holds the class name, such as com.example.orders.OrderEvent.
The consumer side needs more care, and the two properties entries are where most errors come from.
Trusted packages
JsonDeserializer can use the type header to decide which class to build. If it trusted any class name, a malicious producer could make your consumer instantiate arbitrary classes. To prevent this, the deserializer only accepts classes from packages you list in spring.json.trusted.packages. Avoid * outside local experiments.
Default type
spring.json.value.default.type tells the deserializer which class to use when the header is missing or cannot be used. This matters when the producer is not a Spring application, or when it uses a different package name. Without it, deserialization fails.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `The class '...' is not in the trusted packages` | The type header names a package you did not trust | Add the package to `spring.json.trusted.packages` |
| `ClassNotFoundException` for a producer's class | The consumer does not have that class, so the header is useless | Set `spring.json.value.default.type` |
| `Can't convert value of class ... to class StringSerializer` | The key type does not match the configured serializer | Make the `KafkaTemplate` generics and serializers agree |
| Events for one entity arrive out of order | Records were sent without a key, or with differing keys | Use a stable key such as the order ID |
A poison record, meaning one that cannot be deserialized, is a special problem. The failure happens inside the consumer's poll, before your listener runs, so the same bytes can be fetched again and again. A later chapter on error handling shows how to deal with this.
Choosing a key
Pick a key that groups records that must stay ordered, and that has many distinct values. A key with only a few values, such as a country code, can make one partition much busier than the others. Changing the partition count later also changes which partition a given key maps to, so ordering across that change is not guaranteed.
Exercise
A consumer fails with "The class 'com.shop.OrderEvent' is not in the trusted packages", but your own class is com.example.orders.OrderEvent. Which two settings do you change so the consumer reads the records into your class?
Show answer
Set spring.json.value.default.type to com.example.orders.OrderEvent so the deserializer builds your class instead of the class named in the header. Also keep spring.json.trusted.packages limited to com.example.orders. Because the foreign class name is not trusted, the type header is not used and the default type applies.
Exercise
Why does using the order ID as the record key keep one order's events in sequence?
Show answer
The producer hashes the key to choose the partition, so every event with the same order ID goes to the same partition. Kafka guarantees order within a partition, so a consumer sees that order's events in the order they were written.