Kafka-在Consumer中反序列化对象 [英] Kafka - Deserializing the object in Consumer

查看:143
本文介绍了Kafka-在Consumer中反序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在考虑在我们的消息传递中使用Kafka,并且我们的应用程序是使用Spring开发的.因此,我们计划使用spring-kafka.

We are considering to use Kafka in our for messaging and our applications are developed using Spring. So, we have planned to use spring-kafka.

生产者将消息作为HashMap对象放入队列.我们有JSON序列化程序,并且我们假设地图将被序列化并放入队列中.这是生产者配置.

The producer puts the message as HashMap object into the queue. We have JSON serializer and we assumed that the map will be serialized and put into the queue. And here is the producer config.

spring:
  kafka:
    bootstrap-servers: localhost:9092
    producer:
        key-serializer: org.springframework.kafka.support.serializer.JsonSerializer
        value-serializer: org.springframework.kafka.support.serializer.JsonSerializer

另一方面,我们有一个侦听器,用于侦听生产者发布消息的相同主题.这是使用者配置:

On the other hand, we have a listener which listens to the same topic where the producer has published the message. Here is the consumer config:

spring:
   kafka:
       consumer:
            group-id: xyz
            key-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
            value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer

我们的侦听器方法:

  public void listener(SomeClass abx)

我们预计json将被反序列化,并且将生成"SomeClass"类型的对象.但是显然,它引发了反序列化异常.

We were expecting the json will be de-serialized and an object of type "SomeClass" will be generated. But apparently, it throws de-serialization exception.

我们看到的文章很少,建议是做类似的事情:

We saw few articles and the suggestion was to do something like:

 @Bean
  public ConsumerFactory<String, Car> consumerFactory() {
    return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
        new JsonDeserializer<>(Car.class));
  }

我们不想编写一些代码来创建反序列化器.有没有我们所缺少的样板东西?任何帮助将不胜感激!!

We don't want to write some code for creating the Deserializer. Is there any boilerplate thing which we are missing? Any help will be appreciated!!

推荐答案

请参见

您还可以如下配置Spring Kafka JsonDeserializer:

You can also configure the Spring Kafka JsonDeserializer as follows:

spring.kafka.consumer.value-deserializer = org.springframework.kafka.support.serializer.JsonDeserializer

spring.kafka.consumer.properties.spring.json.value.default.type = com.example.发票

spring.kafka.consumer.properties.spring.json.trusted.packages = com.example,org.acme

这篇关于Kafka-在Consumer中反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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