String Boot + Redis - SET and GET String Commands Examples


You have hands-on on the basics of how to download and setup a Spring Boot Application using the Spring Initializr tool into your IDE (any of VS Code, IntelliJ, Eclipse or Netbeans)

Also, have an understanding of adding dependencies to your project which can be Gradle or Maven based.

Step 1: Setting up your Spring Boot + Redis Project Dependencies

    Make sure you add the Redis dependency to your Spring Boot Project.


    dependencies {
    	implementation 'org.springframework.boot:spring-boot-starter-data-redis'



Step 2: Setting Redis Configurations in

    Make sure to add the below properties if you have setup Redis Server on your local machine or a server.

    # Redis configuration

    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;
    public class RedisSetGetDemoApplication implements ApplicationRunner {
        public RedisTemplate redisTemplate;
        public static void main(String[] args) {
  , args);
        public void run(ApplicationArguments args) throws Exception {
            //SET Example: SET message Hello
            redisTemplate.opsForValue().set("message", "Hello");
            //GET Example: 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.




Have Questions? Post them here!