I want to deserialize different objects from one message (from the same topic), and according to object type save/update it to the appropriate table in DB. As Gary mentioned here I can use headers to provide information to which type my object must be deserialized. Can you help with examples of how to achieve this?
See sample-02 here.
Sample 2
This sample demonstrates a simple producer and a multi-method consumer; the producer sends objects of types
Foo1
andBar1
and the consumer receives objects of typeFoo2
andBar2
(the objects have the same field,foo
).
The producer uses a
JsonSerializer
; the consumer uses aByteArrayDeserializer
, together with aByteArrayJsonMessageConverter
which converts to the required type of the listener method argument. We can't infer the type in this case (because the type is used to choose the method to call). We therefore configure type mapping on the producer and consumer side. See theapplication.yml
for the producer side and theconverter
bean on the consumer side.
The
MultiMethods
@KafkaListener
has 3 methods; one for each of the known objects and a fallback default method for others.
Run the application and use curl to send some data:
$ curl -X POST http://localhost:8080/send/foo/bar
$ curl -X POST http://localhost:8080/send/bar/baz
$ curl -X POST http://localhost:8080/send/unknown/xxx
Console:
Received: Foo2 [foo=bar]
Received: Bar2 [bar=baz]
Received unknown: xxx