第一步:导包
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
第二步:配置yaml
![](https://i-blog.csdnimg.cn/blog_migrate/b4a678a77ba86701f817ac397a35f221.png)
第三步:导入序列化--工具类
package com.cheng.config;
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 java.net.UnknownHostException;
@Configuration
public class RedisConfig {//redis 默认序列化 序列化拆箱
//覆盖默认的自动配置
@Bean
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//修改默认的序列化规则
//1.创建序列化规则对象
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
//2.更改默认的序列化规则
template.setDefaultSerializer(jackson2JsonRedisSerializer);
return template;
}
}
第四步:编写Controller
![](https://i-blog.csdnimg.cn/blog_migrate/feb99b3c96e189a3b014e40809cc176d.png)
第五步:测试结果
![](https://i-blog.csdnimg.cn/blog_migrate/90c528d47d7c01b708077a740994efc1.png)