Spring Boot + Apache Kafka Tutorial - #10 - Configure Kafka Producer and Consumer for JSON Message


Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will configure Kafka Producer and Consumer for JSON messages. Also, we will create a User class for serializing and deserializing.

Lecture - #10 - Configure Kafka Producer and Consumer for JSON Message

Transcript:

In this lecture, we will configure Kafka Producer and Consumer for JSON Serializer and Deserializer. Well, in the next upcoming lectures, we will see how to send and receive JSON object as a JSON byte array to and from Apache Kafka. Let's head over to the IntelliJ idea and let's change the application.properties file to configure JSON Serializer and Deserializer for Kafka Producer and Consumer. Well, let me head over to the IntelliJ IDEA, let's go to application.properties file over here. So here you can see we have used StringDeserializer and StringSerializer classes to serialize and deserialize message key right so we're going to keep StringDeserializer and StringSerializer classes from Kafka library for this message key we're not going to change the StringDeserializer and StringSerializer classes for this key, we're going to only change the message value. Okay, so here you can see right now we have StringDeserializer class from Kafka Library to deserializer the value for this Consumer. So let's go ahead and let's change it. So let's use JsonDeserializer from Spring Kafka Library. So let me comment on this and here I want to type the property spring.kafka.consumer.value- deserializer and then the value is org.springframework.kafka.support.serializer.JsonDeserializer Okay. Now we are using JsonDeserializer class from spring provided library, not from the Kafka library. Okay. So this JsonDeserializer is basically created by Spring Kafka library, not from Kafka library. Okay. Now we have configured JsonDeserializer for this Kafka Consumer. So a Consumer will basically, you know, convert JSON byte into the Java object by using this JsonDeserializer Now let's go ahead and let's see how to configure JsonSerializer class for this Kafka Producer. So right now we are using StringSerializer class to serialize the value from the Kafka Library right. So let me simply comment it out this and let me write the property. spring.kafka.producer.value-serializer: then type the value org.springframework .kafka.supoort.serializer.JsonSerializer Okay. It means that this Kafka Producer will basically convert Java object into JSON and then it will write to the Kafka topic. All right. Now we have configured Kafka Consumer and Kafka Producer for JsonDeserializer and JsonSerializer All right, great. Well, in a Kafka Consumer, we can also configure one more property that is spring.kafka .consumer.properties .spring.json.trusted.packages=* It means that Kafka Consumer can JsonDeserializer all the classes from this package. Okay, here star (*) meaning all the classes from this package. All right, perfect. Now we have configured Kafka Consumer and Producer for JsonDeserializer and JsonSerializer. All right, in the next lecture, we'll create a simple POJO class that we can write to the Kafka topic. All right, I will see you in the next lecture.

Comments