使用redis缓存对象会出现下图现象:

键值对都是乱码形式。

解决以上问题:

如果是xml配置的

我们直接注入官方给定的keyserializer,valueSerializer,hashKeySerializer即可:

<bean id="apiRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="apiCacheRedisConnectionFactory">
        <property name="keySerializer">
            <bean
                class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>

        <property name="hashKeySerializer">
            <bean
                class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
        <property name="stringSerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
    </bean>

spring boot 项目配置RedisConfig的时候使用以下方法:

@Configuration
public class RedisConfig {
    @Bean("jsonRedisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(redisConnectionFactory);      //解决日期序列化问题
        ObjectMapper om = new ObjectMapper();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(om);
        template.setDefaultSerializer(genericJackson2JsonRedisSerializer);
        return template;

    }
}

Redis存入中文,取出来是乱码wenti

默认情况下,用redis存入中文,取出时会出现乱码情况,如图:

解决

我们再启动redis时,可以在redis-cli 后面加上 –raw,如图

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。


声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。