Spring Boot | How to publish String messages on Apache Kafka

Last Updated : 14 Mar, 2026

Apache Kafka is a distributed publish–subscribe messaging system that allows applications to send and receive messages between different services. In a Spring Boot application, Kafka can be used to publish string messages to Kafka topics for communication between systems. This helps in building real-time and event-driven applications.

  • Spring Boot can send string messages to Kafka topics using a producer.
  • Messages are simple strings (sequence of characters) sent for processing.
  • Kafka enables communication between different services and applications.

Steps to Implement Spring Boot Kafka String Producer

Step 1: Create Spring Boot Project

Go to Spring Initializr and create a new project.

  • Project: Maven
  • Language: Java
  • Dependencies: Spring Web, Spring for Apache Kafka

Download the project and import it into your IDE.

Step 2: Configure Kafka in application.properties

Open application.properties and add the Kafka server configuration.

spring.kafka.bootstrap-servers=localhost:9092

This connects the Spring Boot application to the Kafka broker.

Step 3: Create Kafka Configuration Class

Create a configuration class KafkaConfig.java to configure KafkaTemplate.

Java
@Configuration
public class KafkaConfig {

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate(
            ProducerFactory<String, String> producerFactory) {
        return new KafkaTemplate<>(producerFactory);
    }
}

This class enables sending messages to Kafka topics

Step 4: Create Kafka Producer (Publish String Message)

Create a controller class KafkaProducerController.java to send messages to a Kafka topic.

Java
package com.example.kafka.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class KafkaProducerController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    private static final String TOPIC = "message-topic";

    @GetMapping("/publish")
    public String publishMessage() {

        kafkaTemplate.send(TOPIC, "Hello Kafka from Spring Boot");

        return "Message Published Successfully";
    }
}

Step 5: Start Kafka Services

Start the required Kafka services:

  1. Start Zookeeper Server
  2. Start Kafka Server

Step 6: Create Kafka Topic

For Mac / Linux:

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic message-topic

For Windows:

.\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic message-topic

Step 7: Run Spring Boot Application

  • Start the main Spring Boot class.
  • Send a request to publish the message.

Step 8: Verify Output

  • The string message will be published to the Kafka topic.
  • Consumers subscribed to the topic will receive the message.
  • Calling the API:
  • Checking the message in real time:
Comment