Phil Dobson
Phil Dobson

Reputation: 23

JSON MessageHeaders with headerMode=raw

I have a Spring Cloud Stream (SCS) Kafka producing app configured with Sleuth for tracing. I'm trying to publish a spring-messaging 'GenericMessage' (i.e. MessageHeaders + payload) that needs to be consumed both by:

With headerMode=embeddedHeaders (the default): The tracing entries in MessageHeader are prepended to the message by EmbeddedHeaderUtils before it is published. My non java consumers can't handle this as EmbeddedHeaderUtils doesn't serialize to pure JSON, i.e.

?\n invalid-json-headers { payload }

With headerMode=raw: The MessageHeaders are not sent at all, only the payload is serialized. i.e. { payload }

I really just want to publish the whole GenericMessage as it was created, including the trace + span IDs that Sleuth adds, i.e.:

{"headers": {"id": "x", "trace": "y", "span": "z"}, "payload": { ... }}

Is there any way to achieve this other than publishing to one topic for SCS consumers, and another with just the payload?

Upvotes: 2

Views: 1196

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121282

So, publish that message as a payload of an outbound message:

return MessageBuilder.withPayload(message).build();

This way your message will be serialized as a payload and you can access to the required headers after the proper deserialization and casting payload to the Message.

Upvotes: 1

Related Questions