Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I programaticaly set JsonDeserializer TypeValue method in Spring Kafka

So I have not been able to configure the JavaType method for JsonSerializer using only yaml. Not sure the reason yet, but in the meantime how do I set this programatically?

I have seen code for it in the documentation but where exactly does this code need to run?

Spring Kafka: JsonDeserializer doesn't pick up TRUSTED_PACKAGE config

This is what I tried so far. Kafka Spring Deserialzer returnType static method never called

PS I am on Spring Boot 2.1.8 so cannot easily use the newer way with just properties right now.

like image 910
Roger Alkins Avatar asked Mar 01 '26 14:03

Roger Alkins


1 Answers

To get this functionality with an old Boot release, you will need to copy the code from the 2.5 JsonDeserializer into a custom deserializer - the typeFunction was added in 2.5.

When creating the deserializer programmatically, you must add it to the consumer factory directly. See the documentation.

For more complex or particular cases, the KafkaConsumer (and, therefore, KafkaProducer) provides overloaded constructors to accept Serializer and Deserializer instances for keys and values, respectively.

When you use this API, the DefaultKafkaProducerFactory and DefaultKafkaConsumerFactory also provide properties (through constructors or setter methods) to inject custom Serializer and Deserializer instances into the target Producer or Consumer. Also, you can pass in Supplier or Supplier instances through constructors - these Supplier s are called on creation of each Producer or Consumer.

Create a DefaultKafkaConsumerFactory @Bean (overriding Boot's default).

like image 169
Gary Russell Avatar answered Mar 03 '26 02:03

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!