Java Spring Boot REST Server with Redis and Kafka
đź•“ 40 minutes
#
What you’ll learnHow to setup your application for :
- connecting to Redis,
- connecting to Kafka and publishing messages to its topic,
- getting data from REST API,
- 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 user authorization roles. As roles are stored in Redis key-value store, we need a client configuration for our component. Any access to information about user roles should be logged in a Kafka topic, so we need a Kafka client configuration too.

#
Project sourceThis example project can be cloned from: http://gitlab.factory.codenow-control.codenow.com/public-docs/java-spring-boot-demo/java-spring-boot-rest-server-with-kafka-and-redis.git
#
Prerequisites- Prepare your local development environment for CodeNOW with Spring Boot.
- Follow the tutorial instructions in the Java Spring Boot Local Development tutorial.
- Run Kafka and Redis locally.
- You can run them directly or using docker compose.
- The configuration file for docker-compose can be downloaded from the link that can be found 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 UserAuthorizationResponse, which is a simple POJO with user roles:
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 Local development manual) and create a new topic client-logging
Add maven dependency to your pom.xml
For more details about Spring Boot Kafka, see: https://spring.io/projects/spring-kafka
Now add this code below to your Application.class to create bean configuration for kafka:
Next prepare the configuration for the Redis client:
For more details about Spring Boot Redis, see: https://spring.io/projects/spring-data-redis
Add maven dependency to your pom.xml.
These dependencies are the starter for using Redis key-value data store with Spring Data Redis and the Lettuce client
Create a new configuration class for redis RedisConfig.class:
Create a new controller and put all the parts together
For more details about Spring Boot controller, see: https://spring.io/guides/gs/rest-service/
Last but not least, append the configuration for Kafka and Redis to config/application.yaml
Note that this configuration depends on your local development setup for Kafka and Redis and can be different case-by-case
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
- For correct setup, check the Readme.md in the project root or see the tutorial Java Spring Boot Local Development, section Prepare local development IDE

#
Deploy to CodeNOWIf 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.
- Check Get New Redis and Get New Apache Kafka for setup in the CodeNOW environment.
- Make sure to change the application.yaml properties from the local to the production setup.
- For more information about application deployment, see the Application Deployment and Deployment Monitoring tutorials.
#
What’s next?See our other developer tutorials: