为什么使用RedisTemplate保存的数据,使用StringRedisTemplate无法获取

1、为什么使用RedisTemplate保存的数据,使用StringRedisTemplate无法获取?

是因为RedisTemplate默认使用JDK序列化(设置默认序列号代码位置:org.springframework.data.redis.core.RedisTemplate#afterPropertiesSet)

org.springframework.data.redis.serializer.JdkSerializationRedisSerializer#JdkSerializationRedisSerializer(java.lang.ClassLoader)

 而StringRedisTemplate,默认使用StringRedisSerializer序列化处理,

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.data.redis.core;

import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializer;

public class StringRedisTemplate extends RedisTemplate<String, String> {
    public StringRedisTemplate() {
        this.setKeySerializer(RedisSerializer.string());
        this.setValueSerializer(RedisSerializer.string());
        this.setHashKeySerializer(RedisSerializer.string());
        this.setHashValueSerializer(RedisSerializer.string());
    }

    public StringRedisTemplate(RedisConnectionFactory connectionFactory) {
        this();
        this.setConnectionFactory(connectionFactory);
        this.afterPropertiesSet();
    }

    protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
        return new DefaultStringRedisConnection(connection);
    }
}

2、测试案例:

package com.cn.dl.springbootdemo.controller;

import com.cn.dl.springbootdemo.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    @RequestMapping(value = "test")
    public void selectUserInfo() {
        User user = new User();
        user.setName("yanshao");
        user.setAge(28);
        redisTemplate.opsForValue().set("yanshao",user,3, TimeUnit.MINUTES);

        System.out.println("redisTemplate>>>" + redisTemplate.opsForValue().get("yanshao"));
        System.out.println("stringRedisTemplate>>>" + stringRedisTemplate.opsForValue().get("yanshao"));
    }

}
redisTemplate>>>User(name=yanshao, age=28, price=null, address=null)
stringRedisTemplate>>>null

3、如何解决?

自定义RedisTemplate序列化方式,即使用Jackson2JsonRedisSerializer

package com.cn.dl.springbootdemo.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

@SpringBootConfiguration
public class RedisTemplateConfig {

    @Bean
    public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer<T> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();

        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(om);

        redisTemplate.setKeySerializer(RedisSerializer.string());

        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.setHashKeySerializer(RedisSerializer.string());

        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }
}
package com.cn.dl.springbootdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.dl.springbootdemo.bean.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisTemplate<String,User> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping(value = "test")
    public void selectUserInfo() throws IOException {
        User user = new User();
        user.setName("yanshao");
        user.setAge(28);
        redisTemplate.opsForValue().set("yanshao",user,3, TimeUnit.MINUTES);
        User userInfo = redisTemplate.opsForValue().get("yanshao");
        System.out.println("redisTemplate>>>" + JSONObject.toJSONString(userInfo));

        System.out.println("stringRedisTemplate>>>" + stringRedisTemplate.opsForValue().get("yanshao"));

        stringRedisTemplate.opsForValue().set("yanshao",objectMapper.writeValueAsString(user),3,TimeUnit.MINUTES);

        userInfo = objectMapper.readValue(stringRedisTemplate.opsForValue().get("yanshao"),User.class);
        System.out.println("stringRedisTemplate>>>" + JSONObject.toJSONString(userInfo));
    }

}
redisTemplate>>>{"age":28,"name":"yanshao"}
stringRedisTemplate>>>["com.cn.dl.springbootdemo.bean.User",{"name":"yanshao","age":28,"price":null,"address":null}]
stringRedisTemplate>>>{"age":28,"name":"yanshao"}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

燕少༒江湖

给我一份鼓励!谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值