Kafka - 反序列化消费者中的对象 [英] Kafka - Deserializing the object in 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.Invoice
spring.kafka.consumer.properties.spring.json.trusted.packages=com.example,org.acme
这篇关于Kafka - 反序列化消费者中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!