Spring Boot - Create and Configure Topics in Apache Kafka Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Topics are a special and essential component of Apache Kafka that are used to organize events or messages. In other words, Kafka Topics enable simple data transmission and reception across Kafka Servers by acting as Virtual Groups or Logs that store messages and events in a logical sequence. In this article, we will discuss about how to configure Kafka topics from the spring boot application. Step by Step Implementation First, our spring boot project needs Kafka dependencies. Include this part in your pom.xml file. Any Kafka version is accessible. We would prefer that you use the most recent Kafka client. For additional information on the Kafka version. Java <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.9.0</version> <scope>compile</scope> </dependency> Create a class with whatever name you prefer, but I used TopicConfiguration.java. We need to convert this class into a bean, and since we are configuring Kafka topics, we included the @Configuration annotation from Spring Boot. Java import java.io.*; @Configuration public class TopicConfiguration { } The process of adding new topics to our broker is handled by a KafkaAdmin bean. A KafkaAdmin bean is automatically registered with Spring Boot. The KafkaAdmin bean has to be explicitly registered for non-Spring Boot applications. The next step is to construct a new bean that tells KafkaAdmin what our Kafka URL is. BOOTSTRAP_SERVERS_CONFIG: Host and port on where Kafka broker is running. BOOTSTRAP_SERVERS_CONFIG: <kafka_url>:<port> Java @Bean public KafkaAdmin admin() { Map<String, Object> configs = new HashMap<>(); configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); return new KafkaAdmin(configs); } We can now configure and create Kafka topics. A lot of various settings can be used right here while creating a topic. For more information about TopicBuilder. Java import java.io.*; @Bean public NewTopic topic1() { return TopicBuilder.name("TOPIC-1") .partitions(1) .replicas(1) .build(); } @Bean public NewTopic topic2() { return TopicBuilder.name("TOPIC-2") .partitions(1) .replicas(1) .build(); } File: TopicConfiguration.java Java import java.io.*; @Configuration public class TopicConfiguration { @Bean public KafkaAdmin admin() { Map<String, Object> configs = new HashMap<>(); configs.put( AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); return new KafkaAdmin(configs); } @Bean public NewTopic topic1() { return TopicBuilder.name("TOPIC-1") .partitions(1) .replicas(1) .build(); } @Bean public NewTopic topic2() { return TopicBuilder.name("TOPIC-2") .partitions(1) .replicas(1) .build(); } } In the Springboot application, Kafka topics can be created and configured this way. The spring boot application will automatically create Kafka topics on the specified Kafka broker when it is launched. To get the topic configuration details on the server, run this command. kafka-topics.sh --bootstrap-server localhost:9092 --topic <topic_name> --describe Output: This is the output from the server. Comment More infoAdvertise with us Next Article Spring Boot - Create and Configure Topics in Apache Kafka M malavmevada Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2022 Java-Spring-Boot Apache Kafka Java +1 More Practice Tags : Java Similar Reads 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 Spring Boot | How to consume JSON messages using Apache Kafka Apache Kafka is a stream processing system that lets you send messages between processes, applications, and servers. In this article, we will see how to publish JSON messages on the console of a Spring boot application using Apache Kafka. In order to learn how to create a Spring Boot project, refer 3 min read Spring Boot | How to consume string messages using Apache Kafka Apache Kafka is a publish-subscribe messaging queue used for real-time streams of data. A messaging queue lets you send messages between processes, applications, and servers. In this article we will see how to send string messages from apache kafka to the console of a spring boot application. Appro 3 min read Spring Boot | How to publish String messages on Apache Kafka 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, r 2 min read Spring Boot | How to publish JSON messages on Apache Kafka 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, ref 4 min read Spring Boot - Consume JSON Object From Kafka Topics Apache Kafka is a publish-subscribe messaging system. A messaging system lets someone is sending messages between processes, applications, and servers. Broadly Speaking, Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. Applications may connect 4 min read Like