8
votes

I am trying 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 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

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?

Apologies if these are stupid questions - I'm trying to understand the correct way of setting it up rather than "hacking it till it kinda works".

1
I was using Producer<> and Consumer<> from kakfa library. So while creating those, i used key and value serializers as (StringSerializer) from kafka library. I read in couple of blogs to use StringSerializer. Converted json to string on inserting to queue and vice versa after reading the message).Rohith K

1 Answers

6
votes
  1. <String, Object>

  2. JsonSerializer

  3. The message converter is only used when using the send that takes a Message<?> and with a JsonSerializer you should use the default one.