A hands-on Spring Boot project for learning Apache Kafka — covering producers, consumers, custom serialization, and REST-based message publishing.
This project is a practical learning resource for integrating Apache Kafka with Spring Boot. It demonstrates how to configure a Kafka producer and consumer from scratch, serialize/deserialize custom Java objects using JSON, and expose a REST API to publish messages to a Kafka topic.
- ⚙️ Custom Kafka Producer — Configured with
KafkaTemplateandJsonSerializerfor sending customMessageobjects - 👂 Kafka Consumer / Listener — Uses
@KafkaListenerwithConcurrentKafkaListenerContainerFactoryto consume messages from a topic - 🔄 JSON Serialization — Custom objects serialized/deserialized using Spring Kafka's
JsonSerializer&JsonDeserializer - 🌐 REST API —
POST /api/v1/messagesendpoint to publish messages to thesyscomzKafka topic - 🏃 CommandLineRunner — On startup, automatically sends 100 test messages to Kafka to verify the setup
- 🔧 Trusted Package Configuration — Consumer factory explicitly trusts
com.syscomzfor safe deserialization
learning-apache-kafka/
├── src/
│ └── main/
│ ├── java/com/syscomz/
│ │ ├── KafkaApplication.java # Entry point + CommandLineRunner (sends 100 messages on startup)
│ │ ├── KafkaListeners.java # @KafkaListener consumer for the "syscomz" topic
│ │ ├── Message.java # Custom message record/model
│ │ ├── MessageController.java # REST controller — POST /api/v1/messages
│ │ ├── MessageRequest.java # Request DTO
│ │ └── config/
│ │ ├── KafkaProducerConfig.java # Producer factory + KafkaTemplate bean
│ │ └── KafkaConsumerConfig.java # Consumer factory + listener container factory
│ └── resources/
│ └── application.properties # Kafka bootstrap server config
├── pom.xml
└── README.md
| Technology | Version | Purpose |
|---|---|---|
| Java | 17 | Programming Language |
| Spring Boot | 3.0.3 | Application Framework |
| Spring Web | 2.7.6 | REST API |
| Spring Kafka | 3.0.3 | Kafka Integration |
| Apache Maven | Latest | Build Tool |
- ☕ Java 17+
- 🐳 Docker (recommended for running Kafka locally)
- 📦 Apache Maven or use the included
mvnwwrapper
The easiest way is via Docker:
docker run -d --name kafka \
-p 9092:9092 \
-e KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181 \
bitnami/kafka:latestOr use a docker-compose.yml with Zookeeper + Kafka.
git clone https://github.com/bdostumski/learning-apache-kafka.git
cd learning-apache-kafka./mvnw spring-boot:runOn startup, CommandLineRunner will automatically send 100 messages to the syscomz topic. Watch the console for consumer output:
Listener received data: Hello kafka 0 :)
Listener received data: Hello kafka 1 :)
...
curl -X POST http://localhost:8080/api/v1/messages \
-H "Content-Type: application/json" \
-d '{"message": "Hello from REST!"}'| Method | Endpoint | Description | Body |
|---|---|---|---|
POST |
/api/v1/messages |
Publish a message to Kafka | { "message": "your text" } |
| Property | Default | Description |
|---|---|---|
spring.kafka.bootstrap-servers |
localhost:9092 |
Kafka broker address |
- Kafka Topics — Messages are published to and consumed from the
syscomztopic - Producer Configuration —
ProducerFactorywithStringSerializer(key) +JsonSerializer(value) - Consumer Configuration —
ConsumerFactorywithStringDeserializer+JsonDeserializerwith trusted packages - Listener Container Factory —
ConcurrentKafkaListenerContainerFactoryfor concurrent message processing - Custom Object Messaging — Sending and receiving a custom
Messagerecord with a timestamp
This project is open source and available for learning purposes.