Insert ObjectMapper into Kafka Spring Serializer / Deserializer

I am using Spring Kafka 1.1.2-RELEASE with Spring Boot 1.5.0 RC, and I have configured a custom value serializer / deserializer class that extends org.springframework.kafka.support.serializer.JsonSerializer/ org.springframework.kafka.support.serializer.JsonDeserializer. These classes use the Jackson ObjectMapper, which can be provided through the constructor.

Is there any way to add ObjectMapper from my Spring context? I already have ObjectMapper configured, which I would like to reuse in the serializer / deserializer.

+4
source share
1 answer

JsonSerializer JsonDeserializer @Bean s. ObjectMapper. beans DefaultKafkaProducerFactory DefaultKafkaConsumerFactory bean:

    @Bean
    public ProducerFactory<Integer, String> producerFactory() {
        DefaultKafkaProducerFactory<Integer, String> producerFactory = 
                new DefaultKafkaProducerFactory<>(producerConfigs());
        producerFactory.setValueSerializer(jsonSerializer());
        return producerFactory;
    }
+4

Source: https://habr.com/ru/post/1667668/


All Articles