Java Spring Boot REST Server with Kafka Consumer
🕓 40 minutes
#
What you’ll learnHow to setup your application for :
- connecting to Kafka consuming messages from its’ topic,
- providing data to REST API.
In this tutorial, we will create a simple java component with the java spring-boot scaffolder. We want to expose a single REST endpoint for getting client application logs. Logs are published in Kafka topic, so we need a Kafka client configuration as well. Inside ClientAplicationLogs component is simple logic, which stores last 10 messages from Kafka and is available to return them via REST API.

#
Project sourceThis example project can be cloned from: http://gitlab.factory.codenow-control.codenow.com/public-docs/client-authorization-demo/client-application-logs.git
#
Prerequisites- Prepare your local development environment for CodeNOW with Java Spring Boot.
- Follow the tutorial instructions in Java Spring Boot Local Development.
- Run Apache Kafka locally.
- Use docker compose as described in the section Docker compose and third-party tools of the Java Spring Boot Local Development tutorial.
- Create a new component
- For details see the section Prerequisites of the Java Spring Boot Local Development tutorial.
#
StepsOpen your IDE, import the created component and start coding:
Define the message payload. Here is an example of the Log, which is a simple POJO with basic client data:
generate getters and setters with your IDE
Next prepare the configuration for the Kafka logging client:
Go to the Kafka administration console (http://localhost:9000 if using Kafdrop from our Java Spring Boot Local Development manual) and create a new topic client-logging
Add maven dependency to your pom.xml
For more details about spring-kafka, see: https://spring.io/projects/spring-kafka
Now let’s create simple cache queue storing only last couple of messages. Field ‘limit’ is configured with spring property log.cache.size (see below):
Next step is using spring-boot kafka autoconfiguration and create new kafka listener for logs
‘spring.kafka.topic.name’ is configured via spring properties
Next, create a new controller and put all the parts together
For more detail about the spring REST controller, see: https://spring.io/guides/gs/rest-service/
Last but not least, replace configuration in config/application.yaml with following code:
Note that this configuration depends on your local development setup for Kafka and can differ case-by-case. For your local configuration is probably required to uncomment ‘bootstrap-servers: localhost:29092’ and comment the line above.
Make sure you follow yaml syntax (especially whitespaces)
Try to build and run the application in your IDE. After startup, you should be able to access your new controller’s swagger: http://localhost:8080/swagger/index.html

#
What’s next?If your code works in the local development, you are ready to push your changes to GIT and try to build and deploy your new component version to the CodeNOW environment. For more information see Application Deployment and Monitoring, just make sure to change the application.yaml properties from the local to the production setup.
- Check Get New Apache Kafka for setup in the CodeNOW environment.