0

I use spring-boot (2.2.4), spring-kafka (2.3.5), Gradle and Kotlin. I want to use headers for mapping types, but I don't understand how do it...

I see to spring-kafka reference and write next code:

My config:

@Configuration
class KafkaProducerConfig {

fun defaultConfig(): HashMap<String, Any> {
    var config = HashMap<String, Any>()
    config[ProducerConfig.BOOTSTRAP_SERVERS_CONFIG] = "localhost:9092"
    config[ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG] = IntegerSerializer::class.java
    config[ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG] = JsonSerializer::class.java
    config[JsonSerializer.TYPE_MAPPINGS] = "order:${Order::class.java}, client:${Client::class.java}"

    return config
}

@Bean
fun orderTemplate(): KafkaTemplate<Int, Order> {
    val factory = DefaultKafkaProducerFactory<Int, Order>(defaultConfig(),
            IntegerSerializer(),
            JsonSerializer<Order>())
    return KafkaTemplate(factory)
}
}

I send data:

@Service
class ProducerService(@Autowired val orderSender: KafkaTemplate<Int, Order> { 

@Scheduled(fixedDelay = 2000)
fun send() {
    orderSender.send("order.t", 1, Order())
}

I get the following error:

Magic v1 does not support record headers

Please help me.

  • You need to either upgrade your broker or stop sending headers. See https://stackoverflow.com/a/50572151/6346531 for stopping serialization headers & https://stackoverflow.com/a/76118062/6346531 for stopping tracing headers. – aksh1618 Apr 27 '23 at 08:21

1 Answers1

0

It means your Kafka broker is too old (< 0.11.0.0) to support headers.

The JSON serializer (by default) adds type information to record headers so the receiving system has some hints about how to deserialize.

If you MUST use such an old broker, you can turn off sending headers with

/**
 * Set to false to disable adding type info headers.
 * @param addTypeInfo true to add headers.
 * @since 2.1
 */
public void setAddTypeInfo(boolean addTypeInfo) {
    this.addTypeInfo = addTypeInfo;
}

on the serializer.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179