问题
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.
回答1:
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).
来源:https://stackoverflow.com/questions/63275816/how-to-i-programaticaly-set-jsondeserializer-typevalue-method-in-spring-kafka