springboot集成redis序列化的问题
2021-04-11 02:28
标签:spring ase pre hang 设置 iss bean gobject html 在springboot中RedisTemplate默认使用的是Java本地的序列化(JdkSerializationRedisSerializer)方式 也去看了一下官方文档(https://docs.spring.io/spring-data/redis/docs/2.3.1.RELEASE/reference/html/#redis:serializer),里面写到建议转成JSON格式,如下图所示 ![image-20200722160753602]( 使用默认的序列化方式返回的结果会包含类的信息: 使用Jackson2JsonRedisSerializer序列化结果: 具体代码实现: 配置类 redis工具类 测试类 springboot集成redis序列化的问题 标签:spring ase pre hang 设置 iss bean gobject html 原文地址:https://www.cnblogs.com/JackQiang/p/13362444.htmlspringboot集成redis序列化的问题
public void afterPropertiesSet() {
super.afterPropertiesSet();
boolean defaultUsed = false;
if (this.defaultSerializer == null) {
this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader());
}
if (this.enableDefaultSerializer) {
if (this.keySerializer == null) {
this.keySerializer = this.defaultSerializer;
defaultUsed = true;
}
if (this.valueSerializer == null) {
this.valueSerializer = this.defaultSerializer;
defaultUsed = true;
}
if (this.hashKeySerializer == null) {
this.hashKeySerializer = this.defaultSerializer;
defaultUsed = true;
}
if (this.hashValueSerializer == null) {
this.hashValueSerializer = this.defaultSerializer;
defaultUsed = true;
}
}
if (this.enableDefaultSerializer && defaultUsed) {
Assert.notNull(this.defaultSerializer, "default serializer null and not all serializers initialized");
}
if (this.scriptExecutor == null) {
this.scriptExecutor = new DefaultScriptExecutor(this);
}
this.initialized = true;
}
Book(id=1, name=情人, price=0.0, author=jack)
从redis-cli查询:
127.0.0.1:6379> get book:2
"\xac\xed\x00\x05sr\x00 com.jack.springbootlearn.bo.Book/\xe3IUI\xb5\xab\xf4\x02\x00\x04J\x00\x02idD\x00\x05priceL\x00\x06authort\x00\x12Ljava/lang/String;L\x00\x04nameq\x00~\x00\x01xp\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00t\x00\x04jackt\x00\x06\xe6\x83\x85\xe4\xba\xba"
{id=1, name=情人, price=0.0, author=jack}
从redis-cli中查询:
"{\"id\":1,\"name\":\"\xe6\x83\x85\xe4\xba\xba\",\"price\":0.0,\"author\":\"jack\"}"
/**
* @Description:
* @Author: ZhangQiang
* @CreateDate: 2020/7/8 16:24
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate
@Component
public class RedisUtil {
@Autowired
private RedisTemplate
@SpringBootTest
public class RedisTest {
@Autowired
private RedisUtil redisUtil;
@Test
public void setTest(){
Book book = new Book();
book.setName("情人");
book.setId(1);
book.setAuthor("jack");
redisUtil.set("book:2",book);
}
@Test
public void get(){
String s = redisUtil.get("book:1");
System.out.println(s);
/*String s1 = redisUtil.get("book:2");
System.out.println(s1);*/
}
}
文章标题:springboot集成redis序列化的问题
文章链接:http://soscw.com/index.php/essay/74072.html