Spring Using Kafka JsonSerializer

I try to follow the instructions here:

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

To set up a KafkaTemplate that can serialize and send some simple Java POJOs that I have. But I found the documentation vague and confusing, especially this part:

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

...

Although the Serializer / Deserializer API is fairly simple and flexible from the low level of Kafka Consumer and Producer, there is not enough message level where KafkaTemplate and @KafkaListener are present.

...

MessageConverter can be entered directly into the KafkaTemplate instance and through the AbstractKafkaListenerContainerFactory bean definition for @ Property KafkaListener.containerFactory ()

So my question is:

  • What is the type of my KafkaTemplate? Is it KafkaTemplate<String, Object>? Or is it KafkaTemplate<String, String>?
  • What is my Serializer Class? Is it StringSerializer, or is it JsonSerializer?
  • Do I use kafkaTemplate.setMessageConverter(new StringJsonMessageConverter())when creating my KafkaTemplate bean?

Sorry if these are stupid questions - I'm trying to figure out the right way to configure it, and not "hack it until it works."

+4
1
  • <String, Object>

  • JsonSerializer

  • , Message<?>, JsonSerializer .

+4

Source: https://habr.com/ru/post/1659516/


All Articles