Skip to content

Commit 05f10a4

Browse files
GH-3880: Doc for default @KafkaHandler limitations
Fixes: #3880 Add an documentation on how to correctly inject headers in default `@KafkaHandler`. Because of arguments resolution limitations in Spring Messaging, the `@Header` cannot be used on the default `@KafkaHandler`. Instead the more general `@Headers Map<String, Object>` has to be used. To satisfy default `@KafkaHandler` method expectations (usually `Object payload`), the `HandlerAdapter` injects a payload into `providedArgs`. The type of that payload may clash with expected type of the `@Header` argument. See `InvocableHandlerMethod.getMethodArgumentValues()` for details how params are resolved. Signed-off-by: Sanghyeok An <[email protected]> Co-authored-by: Artem Bilan <[email protected]>
1 parent dd30085 commit 05f10a4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/class-level-kafkalistener.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,25 @@ void listen(Object in, @Header(KafkaHeaders.RECORD_METADATA) ConsumerRecordMetad
6262
...
6363
}
6464
----
65+
66+
Also, this won't work as well.
67+
The `topic` is resolved to the `payload`.
68+
69+
[source, java]
70+
----
71+
@KafkaHandler(isDefault = true)
72+
public void listenDefault(String payload, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
73+
// payload.equals(topic) is True.
74+
...
75+
}
76+
----
77+
78+
If there are use cases in which discrete custom headers are required in a default method, use this:
79+
[source, java]
80+
----
81+
@KafkaHandler(isDefault = true)
82+
void listenDefault(String payload, @Headers Map<String, Object> headers) {
83+
Object myValue = headers.get("MyCustomHeader");
84+
...
85+
}
86+
----

0 commit comments

Comments
 (0)