How to produce a json object message into kafka topic using java(spring)?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a popular open-source stream-processing software platform developed by Linkedin and donated to the Apache Software Foundation, written in Scala and Java. It is designed to handle real-time data feeds efficiently. Java, combined with the Spring Framework, particularly Spring Boot with Spring Kafka, offers a robust solution for producing messages to a Kafka topic.
Understanding Kafka Producers
In Kafka, a producer is responsible for publishing data into Kafka topics. The producer sends a data record (message) to a specific topic within the Kafka cluster. From a technical perspective, this involves serialization of the message content into a format that can be sent over the network and stored efficiently within Kafka, typically in a byte format or more commonly, as JSON for flexibility and ease of use.
Setting Up a Kafka Producer in Spring Boot
To start with the Java implementation, you should first set up a Spring Boot application. This can be done easily using Spring Initializr (https://start.spring.io/). Choose Maven or Gradle as your build tool and add 'Spring for Apache Kafka' dependency.
Application Configuration
You'll need to configure Kafka properties in your application.yml or application.properties file:
Creating the Kafka Producer Service
Let's create a service in Spring that will produce JSON messages. First, make sure to include dependency for JSON processing. In Maven, it will look like this:
Then, create a service:
In the above code:
KafkaTemplateis used to send messages to the Kafka topic.YourCustomObjectis a Java class representing the data.- JSON serialization is handled by
ObjectMapper.
Handle Serialization
Data must be serialized into JSON before being sent to Kafka. This is done using the ObjectMapper in the method sendMessage.
Testing the Kafka Producer
For testing, you can either use unit tests with a Kafka test binder or directly run the Spring Boot application and use a controller or command line runner to trigger your messages.
Key Points Summary
| Component | Description | Used For |
| KafkaTemplate | Part of Spring Kafka, provides high-level operations | Sending messages to Kafka topic |
| ObjectMapper | Part of Jackson library for JSON processing | Serializing objects to JSON |
| application.properties | Configuration file for Spring Boot app settings | Defining Kafka and app properties |
Additional Considerations
- Error Handling: Implement proper error handling in your production code to manage scenarios where Kafka is down or unreachable.
- Security: Secure your Kafka endpoints using SSL/TLS to prevent unauthorized data access or tampering.
- Message Ordering: For certain use cases that require a specific order of messages, custom configurations might be necessary.
- Scalability: Depending on the volume of data, consider partitioning your topics, or scaling your Kafka producers and consumers accordingly.
By following these guidelines, you can effectively produce JSON messages into Kafka topics using Spring Boot with Java. This capability is central to building robust data pipelines and streaming applications that leverage Kafka's high throughput and scalability.
Related reading
- How to produce Kafka Events in Django the right way
- How to produce Kafka messages with JSON format in Python
- How to produce messages to selected partition using kafka-console-producer?
- How to produce messages with headers in Kafka 0.11 using console producer?
- How to programmatically close a JFrame?
- How to properly handle expected errors in Hystrix fallback?
- How to programmatically check if Kafka Broker is up and running in Python
- How to properly implement kafka consumer as a background service on .NET Core

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.