是因为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 {
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);
}
}
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
自定义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 RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer 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 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"}