Spring Boot | How to publish JSON messages on Apache Kafka Last Updated : 21 Jun, 2020 Comments Improve Suggest changes Like Article Like Report Apache Kafka is a publish-subscribe messaging system. A messaging queue lets you send messages between processes, applications, and servers. In this article, we will see how to send JSON messages to Apache Kafka in a spring boot application. In order to learn how to create a spring boot project, refer to this article. The full-form of JSON is JavaScript Object Notation. JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. Though it is derived from a subset of JavaScript, yet it is Language independent. It is a complete language-independent text format. The following steps can be followed in order to publish JSON messages to Apache Kafka: Go to spring initializr and create a starter project with following dependencies: Spring Web Spring for Apache Kafka Open the project in an IDE and sync the dependencies. In this article, we would be creating a student model where we would be posting the student details. Therefore, create a model class Student. Add data members and create constructor and create getters and setters. The following is the implementation of the student class: Java // Java program to implement a // student class // Creating a student class public class Student { // Data members of the // student class int id; String firstName; String lastName; // Constructor of the student // class public Student(int id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } // Implementing the getters // and setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } Now, create a new class Controller with the annotation @RestController. Create a GET API and initialize KafkaTemplate with parameter as string and model class object. The following is the implementation of the controller: Java // Java program to implement a // controller @RestController @RequestMapping("gfg") public class UserResource { @Autowired private KafkaTemplate<String, Student> kafkaTemplate; private static final String TOPIC = "StudentExample"; @GetMapping("/publish/{id}/" + "{firstName}/{lastName}") public String post( @PathVariable("id") final int id, @PathVariable("firstName") final String firstName, @PathVariable("lastName") final String lastName) { kafkaTemplate.send( TOPIC, new Student( id, firstName, lastName)); return "Published successfully"; } } Create a class StudentConfig with the annotation @Configuration. In this class we will serialize the object of the model class. Java // Java program to serialize the // object of the model class @Configuration public class StudentConfig { @Bean public ProducerFactory<String, Student> producerFactory() { // Create a map of a string // and object Map<String, Object> config = new HashMap<>(); config.put( ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092"); config.put( ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); config.put( ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); return new DefaultKafkaProducerFactory<>(config); } @Bean public KafkaTemplate<String, Student> kafkaTemplate() { return new KafkaTemplate<>( producerFactory()); } } Now, start zookeeper and Kafka server. We need to create a new topic with the name StudentExample. To do so, open a new command prompt window and change directory to the Kafka folder. Now, create a new topic using the command given below: For Mac and Linux: bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic topic_name For Windows: .\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic topic_name Now to see the messages on the Kafka server in the real-time, use the command below: For Mac and Linux: bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_name --from-beginning For Windows: .\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic topic_name --from-beginning Run the application and call the API as: localhost:8080/gfg/publish/{id}/{first name}/{last name} Note: If a different port has been used, then replace the port with 8080. Output: Calling the API: Checking the message in real time: Comment More infoAdvertise with us Next Article Spring Boot | How to publish JSON messages on Apache Kafka A AakashYadav4 Follow Improve Article Tags : Java JSON java-advanced Apache Java-Spring +1 More Practice Tags : Java Similar Reads Spring Boot - Scheduling Spring Boot provides the ability to schedule tasks for execution at a given time period with the help of @Scheduled annotation. This article provides a step by step guideline on how we can schedule tasks to run in a spring boot application Implementation:It is depicted below stepwise as follows: St 4 min read Spring Boot - Sending Email via SMTP Spring Boot provides the ability to send emails via SMTP using the JavaMail Library. Here we will be illustrating step-by-step guidelines to develop Restful web services that can be used to send emails with or without attachments. In order to begin with the steps, let us first create a Spring Boot p 5 min read Different Ways to Establish Communication Between Spring Microservices If you are working with a Spring Boot project which involves multiple microservices, You might have felt the need to communicate from microservice m1 to microservice m2. Depending upon business use-cases, this communication can be of synchronous or asynchronous type. Before digging dip further. Let' 3 min read JSON using Jackson in REST API Implementation with Spring Boot When we build REST APIs with Spring Boot, we need to exclude NULL values from the JSON responses. This is useful when we want to optimize the data being transferred, making the response more compact and easier to process for the client.In this article, we are going to learn the approach that is used 4 min read How to encrypt passwords in a Spring Boot project using Jasypt In this article, we will learn how to encrypt data in Spring Boot application config files like application.properties or application.yml. Inside those files, we can encrypt username, password, etc. You often come across developing projects where you have to connect to databases like MongoDB, etc, a 4 min read Containerizing Spring Boot Application Java applications, known for their robustness sometimes become difficult to deploy and manage. This is where containerization comes into the picture. Packaging your Java app into a lightweight and self-contained independent unit can provide many benefits to the developers: PortabilityScalabilityFast 3 min read How to Send Images in Spring Boot without using Servlet and Redundant JSP Codes? Open IntelliJ IDE and in the Controller section of your project inside the Controller class you need to build the Image serve method. The Image serve method looks like below and as you have to collect the Image response use @GetMapping Annotation to collect the image. ImageHandler Controller Method 2 min read How to Create Todo List API using Spring Boot and MySQL? Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se 6 min read Spring Boot - Transaction Management Using @Transactional Annotation The @Transactional annotation is the metadata used for managing transactions in the Spring Boot application. To configure Spring Transaction, this annotation can be applied at the class level or method level. In an enterprise application, a transaction is a sequence of actions performed by the appli 9 min read Spring Boot - Map Entity to DTO using ModelMapper In enterprise applications, we use RESTful services to establish the communication between client and server. The general idea is that the client sends the request to the server and the server responds to that request with some response. Generally, we have three different layers in most of the appli 8 min read Like