springboot集成redis存对象乱码
其实本质上不算是一种乱码,只是序列化之后存储的东西而已,当我们使用RedisTemplete存储对象,时,如果该对象没有被序列化则会报错,序列化之后得到的不是自己想要的数据,为了解决这种方法,我们可以使用 StringRedisTemplete
话不多说上代码
默认使用redistemplete时
@SpringBootTest
public class RedisListTest01 {
// @Autowired
// StringRedisTemplate stringRedisTemplate;
@Autowired
RedisTemplate redisTemplate;
@Test
public void test(){
redisTemplate.opsForValue().set("user1",new User(2,"lisi",19));
System.out.println(redisTemplate.opsForValue().get("user1"));
}
}
当我们运行时会发现报错了!提示我们User对象没有序列化。
org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.xxx.redis01.model.User]
.....
Caused by: java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.xxx.redis01.model.User]
at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:43)
at org.springframework.core.serializer.Serializer.serializeToByteArray(Serializer.java:56)
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:60)
... 72 more
Process finished with exit code -1
当我们给User对象序列化之后运行
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
//省略getter setter toString等
}
天啦噜,竟然不乱码了!!! ,但是–当你使用linux 远程连接后查看keys 时你会发现,欸? 这里乱码了。。。
2022-04-01 20:32:07.666 INFO 18640 --- [ main] com.xxx.redis01.RedisListTest01 : No active profile set, falling back to 1 default profile: "default"
2022-04-01 20:32:07.998 INFO 18640 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2022-04-01 20:32:08.000 INFO 18640 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2022-04-01 20:32:08.018 INFO 18640 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 Redis repository interfaces.
2022-04-01 20:32:08.752 INFO 18640 --- [ main] com.xxx.redis01.RedisListTest01 : Started RedisListTest01 in 1.488 seconds (JVM running for 2.517)
User{id=2, name='lisi', age='19'}Process finished with exit code 0
这是什么鬼? 明明是user1 这是啥啊
127.0.0.1:6379> keys *
¬쳵ser1
可以考虑使用StringRedisTemplete
解决方式肯定不是只有一种,只是自己学习记录,,仅供参考
@SpringBootTest
public class RedisListTest01 {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
public void test(){
stringRedisTemplate.opsForValue().set("user1",new User(2,"lisi",19).toString());
System.out.println(stringRedisTemplate.opsForValue().get("user1"));
}
}
成功拿到
User{id=2, name='lisi', age='19'}
我们在linux下看看,同样ok
127.0.0.1:6379> keys *
user1
127.0.0.1:6379> get user1
User{id=2, name='lisi', age='19'}
127.0.0.1:6379>
当然还有另一种方式,存入JSON格式,这里就不放代码了.
解决redis乱码问题
直接添加一个配置类就行了
@Configuration
public class Config {
@Autowired
private RedisTemplate redisTemplate;
@Bean
public RedisTemplate redisTemplateInit() {
//设置序列化Key的实例化对象
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置序列化Value的实例化对象
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。