Shakun
Shakun

Reputation: 11

How to add headers to a message before posting to Kafka Topic using Spring Cloud Stream

I just realised that the message once received on Kafka Topic looses the headers. Is there a way to add headers to the message before posting it to Kafka Topic and then read it at the consumer? I am using Java 11, Spring Cloud Hoxton.SR6, Spring 2.2.4, kafka_2.13-2.6.0. Thanks in advance!

Upvotes: 0

Views: 427

Answers (1)

Shakun
Shakun

Reputation: 11

I think posted the question a little too early. Its pretty simple. Get the request headers from HttpServletRequest or HttpRequest(spring) and then while writing to the MessageChannel, call the CopyHeaders method while passing the headers. You can also skip the ones which are not needed. At the consumer, instead of a Custom Object, get the Message object which has all the headers

Producer MessageBuilder.withPayload(message).copyHeaders(headersMap).build(); Consumer processMessage(Message<?> message)

Upvotes: 1

Related Questions