如何在Spring Kafka中以编程方式设置JsonDeserializer的TypeValue方法

huangapple go评论59阅读模式
英文:

How to I programaticaly set JsonDeserializer TypeValue method in Spring Kafka

问题

I have not been able to configure the JavaType method for JsonSerializer using only yaml.

我尚未能够仅使用YAML配置JavaType方法的JsonSerializer。

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?

我在文档中看到了相关代码,但这段代码需要在哪里运行?

This is what I tried so far.

这是我迄今为止尝试过的内容。

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

我使用的是Spring Boot 2.1.8,所以目前无法轻松地只使用属性来使用较新的方法。

英文:

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?

https://stackoverflow.com/questions/60582666/spring-kafka-jsondeserializer-doesnt-pick-up-trusted-package-config

This is what I tried so far.
https://stackoverflow.com/questions/63263947/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.

答案1

得分: 1

要获得旧版 Boot 发布的这个功能,您需要将 2.5 版本的 JsonDeserializer 代码复制到自定义反序列化器中 - typeFunction 是在 2.5 版本中添加的。

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

>对于更复杂或特殊的情况,KafkaConsumer(因此也包括 KafkaProducer)提供了重载的构造函数,用于接受键和值的序列化器和反序列化器实例。

>当您使用此API时,DefaultKafkaProducerFactory 和 DefaultKafkaConsumerFactory 也提供了属性(通过构造函数或setter方法)以将自定义的序列化器和反序列化器实例注入目标 Producer 或 Consumer。此外,您可以通过构造函数传递 Supplier<Serializer> 或 Supplier<Deserializer> 实例 - 这些 Supplier 在创建每个 Producer 或 Consumer 时被调用。

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

英文:

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<Serializer> or Supplier<Deserializer> instances through constructors - these Supplier s are called on creation of each Producer or Consumer.

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

huangapple
  • 本文由 发表于 2020年8月6日 09:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63275816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定