Beginner SpringBoot recording pit using serialization Redis

Dependent added using redis spring Boot

<!--springboot整合redis-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- jackson序列化 -->
<dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
</dependency>

Then configure redis

package com.xx.xxx.xxxx;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration 
public  class RedisConfig { 
    @Bean 
    public RedisTemplate <String, Object> redisTemplate (RedisConnectionFactory Factory) { 

        RedisTemplate <String, Object> Template = new new RedisTemplate <> ();
         // Configure connection factory 
        template.setConnectionFactory (Factory); 

        // Jackson2JsonRedisSerializer used to serialize and deserialize redis value of the values (JDK use default serialization) 
        Jackson2JsonRedisSerializer jacksonSeial = new new Jackson2JsonRedisSerializer (Object. class ); 

        ObjectMapper OM = new new ObjectMapper ();
         //Specifies the sequence of fields, field, get, and SET, and range modifiers, there is the ANY including private and public 
        om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
         // the specified type of the input sequence, non-final class must be modified, the modified final class, such as String, Integer, etc. will run out abnormal 
        om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); 
        jacksonSeial.setObjectMapper (OM); 

        // value takes json serialization 
        template.setValueSerializer ( jacksonSeial);
         // use StringRedisSerializer to serialize and deserialize redis key value 
        template.setKeySerializer ( new new StringRedisSerializer ()); 

        // set the hash key and the value sequence pattern 
        template.setHashKeySerializer ( new new StringRedisSerializer ());
        template.setHashValueSerializer(jacksonSeial);
        template.afterPropertiesSet();

        return template;
    }
}

Write dao file

package com.xx.xxx.xxxx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


@Component
public class RedisDao {

    @Autowired
    RedisTemplate redisTemplate;

    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public String get(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }
}

Test category

package com.xx.xxx;

import com.xx.xxx.xxxx.RedisDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;

@SpringBootTest
class CapApplicationTests {

    @Autowired
    RedisDao redisDao;

    @Test
    void contextLoads() {
        redisDao.set("name", "lord");
        System.out.println(redisDao.get("name"));
    }

}

The above is feasible, but one thing must be noted that the use of time springBoot get value must be value springBoot set, otherwise when deserialization would be an error, error contents com.fasterxml.jackson.core.JsonParseException: Unrecognized token "xxxxxx", xxxxxx to get the value.

In redis client to manually set the time value shown in FIG then spring content values being given in FIG Boot

After using spring Boot set, the value is shown in FIG , through careful observation value springBoot redis was serialized.

And then to value by spring Boot redis, as shown in FIG .

After spring Boot redis after deserialization, as output.

Spring Boot climb above for the beginner pit experience.

Guess you like

Origin www.cnblogs.com/zepc007/p/12331883.html