如何在 Spring Kafka 中以编程方式设置 JsonDeserializer TypeValue 方法 [英] How to I programaticaly set JsonDeserializer TypeValue method in Spring Kafka

查看:42
本文介绍了如何在 Spring Kafka 中以编程方式设置 JsonDeserializer TypeValue 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直无法仅使用 yaml 为 JsonSerializer 配置 JavaType 方法.还不确定原因,但同时我如何以编程方式设置它?

我在文档中看到了它的代码,但是这段代码到底需要在哪里运行?

Spring Kafka:JsonDeserializer 不接受 TRUSTED_PACKAGE 配置

这是我目前尝试过的.Kafka Spring Deserialzer returnType 静态方法从未调用

PS 我在 Spring Boot 2.1.8 上,所以现在不能轻易地使用新的方式只使用属性.

解决方案

要在旧的 Boot 版本中获得此功能,您需要将代码从 2.5 JsonDeserializer 复制到自定义解串器中 -typeFunction 是在 2.5 中添加的.

以编程方式创建反序列化器时,必须直接将其添加到消费者工厂中.请参阅文档.

<块引用>

对于更复杂或特定的情况,KafkaConsumer(以及因此,KafkaProducer)提供重载构造函数以分别接受键和值的 Serializer 和 Deserializer 实例.

<块引用>

当您使用此 API 时,DefaultKafkaProducerFactory 和 DefaultKafkaConsumerFactory 还提供属性(通过构造函数或 setter 方法)以将自定义 Serializer 和 Deserializer 实例注入目标生产者或消费者.此外,您可以通过构造函数传入 Supplier 或 Supplier 实例 - 在创建每个 Producer 或 Consumer 时调用这些 Supplier .

创建一个 DefaultKafkaConsumerFactory @Bean(覆盖 Boot 的默认设置).

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.

解决方案

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).

这篇关于如何在 Spring Kafka 中以编程方式设置 JsonDeserializer TypeValue 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆