如何在 Spring Kafka JsonSerializer 中注入 ObjectMapper bean?

How to inject ObjectMapper bean in Spring Kafka JsonSerializer?

spring-kafka 使用无参数构造函数在 AbstractConfig class 中创建一个 ValueSerializer 实例。

我可以看到 JsonSerializer 有一个 ObjectMapper 构造函数,我想用它来注入预配置的 ObjectMapper bean。

默认 ObjectMapper 在我想删除的响应中包含 null 值。我将 spring.jackson.default-property-inclusion: NON_EMPTY 添加到我的 properties.yml 但由于 Spring 创建了一个默认实例,这对我没有帮助。

有人能给我指出正确的方向吗?

我认为您是对的,但是 属性 设置不正确。我想你想要

spring.jackson.default-property-inclusion=non_null

否则这篇文章https://www.baeldung.com/spring-boot-customize-jackson-objectmapper有很多例子可以解决这个问题。

当您通过 kafka 生产者属性配置序列化程序时,创建序列化程序的是 Kafka,而不是 Spring。

您可以通过构造函数或设置器手动将适当配置的序列化器添加到生产者工厂。

https://docs.spring.io/spring-kafka/docs/current/reference/html/#programmatic-construction