Step 1: Setting up your Spring Boot + Redis Project Dependencies
Make sure you add the Redis dependency to your Spring Boot Project.
build.gradle:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
}
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Step 2: Setting Redis Configurations in application.properties
Make sure to add the below properties if you have setup Redis Server on your local machine or a server.
# Redis configuration
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=redis-db-password
Note: If you are just starting with Redis and trying to learn, its better to setup a free Redis Cloud account and make use of it.
Step 3: Redis SET and GET Command Examples with Spring Boot
Just like working with Spring Boot JDBCTemplate, we have a class called RedisTemplate which we can make use of to perform SET and GET commands via Java code.
To keep the example simple, we will implement ApplicationRunner our class with @SpringBootApplication and override the run() method and write our code there.
package com.example.redisdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootApplication
public class RedisSetGetDemoApplication implements ApplicationRunner {
@Autowired
public RedisTemplate redisTemplate;
public static void main(String[] args) {
SpringApplication.run(RedisSetGetDemoApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
//SET Example: SET message Hello
redisTemplate.opsForValue().set("message", "Hello");
//GET Example: GET message
System.out.println(redisTemplate.opsForValue().get("message"));
}
}
As you would have seen in the above code, to make use of SET, we use the method opsForValue().set() from the RedisTemplate, and similarly opsForValue().get() for GET Command.
Output:
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- list of jars required for hibernate 4.x.x - Java
- How to Show Line Numbers in Google Colab Cells - Google
- Fix: sudo: unable to open Read-only file system - Linux
- Enable macOS Stage Manager - MacOS
- Change CSS Background Opacity with Examples - CSS
- How to Open Finder using Mac Terminal - MacOS
- Python: Split a String into a Dictionary - Python
- How to fix: You will need Google Chrome to install most apps, extensions and themes. - Chrome