This example shows how to use Spring Data with Redis to do CRUD operations. Additionally, it demonstrates using Redis as a message broker.
The code is implemented using Kotlin, but it's easy for Java developers to understand as well.
For more in-depth details, have a look at the tutorials at links below:
- Getting started with Spring Data Redis with Kotlin
- Redis Pub/Sub with Spring Boot
- Server-Sent Events with Spring MVC SseEmitter
- Distributed SSE with Spring SseEmitter and Redis Pub/Sub
- How to handle MethodArgumentNotValidException in Spring Boot
- Spring Boot Redis integration test with Testcontainers in Kotlin
- Spring Boot Swagger documentation using OpenAPI
- Build Docker images in Spring Boot 3
To demonstrate CRUD operations, I use Movie
, Actor
model and created some endpoints to do basic CRUD operations as follows:
- Create Movie
- Update Movie
- Delete Movie
- List Movies
- Create Actor
- Update Actor
- Delete Actor
- List Actors
- Add an Actor to a Movie
The project needs Redis to run. There's a docker-compose
file contains all necessary configuration to run Redis flawlessly.
To use it. Just run:
$ docker-compose -f docker-compose.yml up -d
After that the Redis should be accessible via port 6379
on localhost
Then you can run the application like below:
$ ./mvnw spring-boot:run -Dserver.port=8090
If you have a Redis running in another port or host, simply override below environment variables:
SPRING_REDIS_HOST
SPRING_REDIS_PORT
Or modify application.properties
file.
To interact with the APIs, after running the project, just open http://localhost:8080/swagger-ui/index.html. You should see Swagger to interact with.