I am using Spring Cloud Stream, with RabbitMQ binder. It works great with byte[]
payload and Java native serialization, but I need to work with JSON payload.
Here's my processor class.
@EnableBinding(Processor.class)
public class MessageProcessor {
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public OutputDto handleIncomingMessage(InputDto inputDto) {
// Run some job.
return new OutputDto();
}
}
InputDto
and OutputDto
are POJOs with Jackson annotations.
In your consumer you can add a content type configuration, e.g.
spring.cloud.stream.bindings.input.content-type: application/x-java-object;type=my.package.InputDto
You could also add
spring.cloud.stream.bindings.output.content-type: application/json
to force the outgoing message payload to be JSON (for interop etc.).
Note that "input" and "output" are the binder channel names (i.e. as defined in Processor
in your app).
I think there is a good chance this could be made easier or more automatic, but there is some engineering effort required to make that happen in Spring Cloud. There's an issue in github if you want to follow it: https://github.com/spring-cloud/spring-cloud-stream/issues/156.
To send a message manually to a Spring Cloud Stream you can set the headers up yourself manually (but it's easier to use a Stream). A JSON message looks like this in the Rabbit admin UI:
priority: 0
delivery_mode: 2
headers:
contentType: text/plain
originalContentType: application/json
content_type: text/plain
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With