首页 > 解决方案 > 使用 DeadLetterPublishingRecoverer 发布 Json 反序列化错误不会发布原始有效负载

问题描述

我正在使用 Spring Boot 2.3.1 并希望发布无法使用 DeadLetterPublishingRecoverer 反序列化的记录。

一切看起来都很好,除了原始有效负载没有写入 DLT 主题。相反,我看到它是 Base64 编码的。

在另一篇文章中,我读到这是由 Kafka 模板中使用的 JsonSerializer 引起的,因此我尝试使用不同的模板。但现在我得到一个序列化异常:

org.apache.kafka.common.errors.SerializationException: Can't convert value of class [B to class org.apache.kafka.common.serialization.BytesSerializer specified in value.serializer

使用 StringSerializer 时会发生类似的异常。

我的代码如下所示:

    @Autowired
    private KafkaProperties kafkaProperties;

    private ProducerFactory<String, String> pf() {
        return new DefaultKafkaProducerFactory<>(kafkaProperties.buildProducerProperties());
    }

    private KafkaTemplate<String, String> stringTemplate() {
        return new KafkaTemplate<>(pf(), Collections.singletonMap(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class));
    }

    @Bean
    public SeekToCurrentErrorHandler errorHandler() {

        SeekToCurrentErrorHandler eh = new SeekToCurrentErrorHandler(new DeadLetterPublishingRecoverer(stringTemplate()));
        eh.setLogLevel(Level.WARN);
        return eh;
    }

标签: springspring-bootapache-kafka

解决方案


5分钟后才发现。

我不得不ByteArraySerializer改用。


推荐阅读