您的位置:首页 > 科技 > 能源 > 重生之 SpringBoot3 入门保姆级学习(21、场景整合 Redis 定制对象序列化存储)

重生之 SpringBoot3 入门保姆级学习(21、场景整合 Redis 定制对象序列化存储)

2024/10/6 12:31:57 来源:https://blog.csdn.net/weixin_56050344/article/details/139685479  浏览:    关键词:重生之 SpringBoot3 入门保姆级学习(21、场景整合 Redis 定制对象序列化存储)

重生之 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

image-20240614162730913

http://localhost:8080/person/get

image-20240614162750833

  • 查看 Redis 数据库发现乱码

image-20240614162826274

  • 开始定制

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*/@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);// 将对象转换为 JSON 字符串的序列化工具template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());return template;}}

2、重新启动项目并按上述流程访问 接口

image-20240614164006314

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com