私信  •  关注

Archon

Archon 最近回复了
5 年前
回复了 Archon 创建的主题 » Python将字节写入ReDIS,但Java以异常方式读取它。

因为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;
}