Skip to content
Kafka Is Fun

SpringSpring Kafka Advanced

How do you configure Spring Kafka for JSON serialization with type information?

Reference answer

Use JacksonJsonSerializer and JacksonJsonDeserializer (Spring Kafka 4.x with Jackson 3; JsonSerializer and JsonDeserializer in earlier versions). The serializer adds type information headers by default; set spring.json.add.type.headers=false (for example through spring.kafka.producer.properties) to disable that. On the consumer the deserializer by default trusts only the packages java.util and java.lang, so set spring.json.trusted.packages (through spring.kafka.consumer.properties) to a comma-delimited list of package patterns; * means all. spring.json.value.default.type is only a fallback type used when the record has no type header information; the USE_TYPE_INFO_HEADERS property set to false makes the deserializer ignore headers set by the serializer. If producer and consumer use different class names, spring.json.type.mapping maps short type tokens to classes, for example cat:com.mycat.Cat,hat:com.myhat.Hat.

Expected key concepts: JacksonJsonSerializer, JacksonJsonDeserializer, spring.json.add.type.headers, spring.json.trusted.packages, spring.json.value.default.type, spring.json.type.mapping, java.util, fallback

How did that feel?

Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.