因为Spring Redis有一个默认值
valueSerializer
,它将序列化原始
byte[]
.
byte[] rawValue(Object value) {
if (valueSerializer() == null && value instanceof byte[]) {
return (byte[]) value;
}
return valueSerializer().serialize(value);
}
如此设定
Serializer
到
null
解决了这个问题
@Bean
public RedisTemplate<byte[], byte[]> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// set this false will keep all Serializer null
template.setEnableDefaultSerializer(false);
return template;
}