重生之 SpringBoot3 入门保姆级学习(21、场景整合 Redis 定制对象序列化存储)
- 6.4 定制化
6.4 定制化
需求:保存一个 Person 对象到 redis
- 创建 Person 类
package com.zhong.redis.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* @ClassName : Person
* @Description :
* @Author : zhx
* @Date: 2024-06-14 16:11
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
// implements Serializable 实现序列化接口不然会报错
private Long id;
private String name;
private Integer age;
private Date birthday;
}
- 创建 RedisTestController 接口
package com.zhong.redis.controller;
import com.zhong.redis.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @ClassName : RedisTestController
* @Description :
* @Author : zhx
* @Date: 2024-06-14 15:13
*/
@RestController
public class RedisTestController {
@Autowired // 如果给 redis 中保存数据会使用默认序列化机制,导致 redis 中保存对象不合适
RedisTemplate redisTemplate;
@GetMapping("/person/save")
public String savePerson() {
Person person = new Person(1L,"小钟",23,new Date());
redisTemplate.opsForValue().set("person", person);
return "ok";
}
@GetMapping("/person/get")
public Person getPerson() {
return (Person) redisTemplate.opsForValue().get("person");
}
}
- 浏览器依次访问接口测试
http://localhost:8080/person/save
http://localhost:8080/person/get
- 查看 Redis 数据库发现乱码
- 开始定制
1、创建 RedisConfiguartion 配置类将 对象 序列化为 JSON
package com.zhong.redis.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
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.GenericJackson2JsonRedisSerializer;
/**
* @ClassName : RedisConfiguartion
* @Description :
* @Author : zhx
* @Date: 2024-06-14 16:33
*/
@Configuration
public class RedisConfiguration {
/**
* template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer()); 所有使用 JSON 存储
* @param redisConnectionFactory 自动配置好了连接工程
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 将对象转换为 JSON 字符串的序列化工具
template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
2、重新启动项目并按上述流程访问 接口