编程语言
首页 > 编程语言> > java – Spring Kafka JsonSerializer用法

java – Spring Kafka JsonSerializer用法

作者:互联网

我想按照这里的说明操作:

http://docs.spring.io/spring-kafka/docs/1.1.1.RELEASE/reference/htmlsingle/#_serialization_deserialization_and_message_conversion

设置一个KafkaTemplate,它可以序列化并发送我拥有的一些简单的Java POJO.但我发现文档含糊不清,特别是这一部分:

For this purpose Spring for Apache Kafka also provides
JsonSerializer/JsonDeserializer implementations based on the Jackson
JSON processor. When JsonSerializer is pretty simple and just lets to
write any Java object as a JSON byte[]

Although
Serializer/Deserializer API is pretty simple and flexible from the
low-level Kafka Consumer and Producer perspective, it is not enough on
the Messaging level, where KafkaTemplate and @KafkaListener are
present.

The MessageConverter can be
injected into KafkaTemplate instance directly and via
AbstractKafkaListenerContainerFactory bean definition for the
@KafkaListener.containerFactory() property

所以我的问题是:

>我的KafkaTemplate的类型是什么?它是KafkaTemplate< String,Object>?或者它是KafkaTemplate< String,String>?
>我的是什么
Serializer类?是StringSerializer,还是JsonSerializer?
>做
我在创建KafkaTemplate bean时使用kafkaTemplate.setMessageConverter(new StringJsonMessageConverter())?

抱歉,如果这些都是愚蠢的问题 – 我试图理解正确的设置方式,而不是“黑客攻击直到它有点工作”.

解决方法:

>< String,Object>
> JsonSerializer
>仅当使用发送消息<?>的发送时才使用消息转换器.使用JsonSerializer,你应该使用默认的.

标签:java,serialization,spring,apache-kafka,spring-kafka
来源: https://codeday.me/bug/20190623/1267138.html