From 049ef27aefd733f1bebb97569de92617c5206540 Mon Sep 17 00:00:00 2001 From: Sanghyeok An Date: Thu, 8 May 2025 17:59:20 +0900 Subject: [PATCH] spring-projectsGH-3880: The DefaultHandler resolves an incorrect value for the parameter annotated with @Header. Fixes: #3880 Issue link: #3880 Add an documentation on how to correctly inject headers in default methods of class-level @KafkaListener. Because currently, The DefaultHandler resolve an incorrect value for the parameter annotated with @Header. also, the spring-kafka's intended usage is incompatiable with the design intent of the InvocationHandler, so spring-kafka cannot resolve this issue. Therefore, we will add documentation to prevent users from becoming confused. Signed-off-by: Sanghyeok An --- .../class-level-kafkalistener.adoc | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/class-level-kafkalistener.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/class-level-kafkalistener.adoc index 3fdf026192..919fefd399 100644 --- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/class-level-kafkalistener.adoc +++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/class-level-kafkalistener.adoc @@ -62,3 +62,25 @@ void listen(Object in, @Header(KafkaHeaders.RECORD_METADATA) ConsumerRecordMetad ... } ---- + +Also, this won't work as well. +The `topic` is resolved to the `payload`. + +[source, java] +---- +@KafkaHandler(isDefault = true) +public void listenDefault(String payload, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) { + // payload.equals(topic) is True. + ... +} +---- + +If there are use cases in which discrete custom headers are required in a default method, use this: +[source, java] +---- +@KafkaHandler(isDefault = true) +void listenDefault(String payload, @Headers Map headers) { + Object myValue = headers.get("MyCustomHeader"); + ... +} +----