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

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

问题描述

因此,我无法仅使用yaml为JsonSerializer配置JavaType方法.尚不清楚原因,但与此同时我该如何以编程方式设置此值?

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不会选择TRUSTED_PACKAGE配置

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

This is what I tried so far. Kafka Spring Deserialzer returnType static method never called

我在Spring Boot 2.1.8上使用PS,因此现在无法仅通过属性轻松使用更新的方式.

PS I am on Spring Boot 2.1.8 so cannot easily use the newer way with just properties right now.

推荐答案

要在旧版Boot中获得此功能,您需要将代码从2.5 JsonDeserializer 复制到自定义反序列化器中- typeFunction 已在2.5中添加.

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.

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

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.

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

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.

创建一个 DefaultKafkaConsumerFactory @Bean (替代Boot的默认值).

Create a DefaultKafkaConsumerFactory @Bean (overriding Boot's default).

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

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