Spring Boot | How to publish String 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 string 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 string is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created. And also, java allows explicit typecasting where one type of variable can be forcibly converted into other. For example, the same string message can further be converted into an int or a float if the string consists of only numbers and it needs to be used for some computation. The following steps can be followed in order to publish a string message 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. Now create a new class Controller with the annotation @RestController. This class handles all the RESTful routes. In this class, create a GET API and initialize KafkaTemplate with parameter as string. The following is the implementation of the class: Java // Java program to implement the // controller for the spring // application @RestController @RequestMapping("/kafka") public class Controller { @Autowired KafkaTemplate<String, String> kafkaTemplate; static final String TOPIC = "gfg"; // Implementing a GET method @GetMapping("publish/{message}") public String publish_message( @PathVariable("message") String message) { kafkaTemplate.send(TOPIC, message); return "Message Published on Kafka !"; } } Start zookeeper and Kafka server. Now we need to create a new topic with the name gfg. To do so, open a new command prompt window and change directory to the Kafka directory. 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/kafka/publish/{your message} 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 String messages on Apache Kafka A AakashYadav4 Follow Improve Article Tags : Java java-advanced Apache Java-Spring Practice Tags : Java Similar Reads Spring - RestTemplate Due to high traffic and quick access to services, REST APIs are getting more popular and have become the backbone of modern web development. It provides quick access to services and also provides fast data exchange between applications. REST is not a protocol or a standard, rather, it is a set of ar 7 min read 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 Like