spring Boot使用redis时添加其依赖
<!--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>
然后进行配置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 RedisTemplate<>(); // 配置连接工厂 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om); // 值采用json序列化 template.setValueSerializer(jacksonSeial); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 设置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); template.afterPropertiesSet(); return template; } }
编写dao文件
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); } }
测试类
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")); } }
以上是可行的,但有一点必须注意就是使用springBoot get取值的时候,必须是springBoot set的值,否则在反序列化时会进行报错,报错内容为com.fasterxml.jackson.core.JsonParseException: Unrecognized token "xxxxxx", xxxxxx为get的值。
在redis客户端手动set值的时候如图所示,然后spring Boot取值时报错内容如图
使用spring Boot set值后,其值如图,仔细观察其值经过springBoot redis进行了序列化。
然后再通过spring Boot redis进行取值,如图。
经过spring Boot redis反序列化后,原样输出了。
以上为初学Spring Boot爬坑经历。