鍵值對都是亂碼形式。
如果是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 RedisTemplateObject, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplateObject, Object> template = new RedisTemplateObject, 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; } }
默認(rèn)情況下,用redis存入中文,取出時會出現(xiàn)亂碼情況,如圖:
我們再啟動redis時,可以在redis-cli 后面加上 --raw,如圖
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
標(biāo)簽:臺州 北京 吉安 楊凌 果洛 江蘇 朝陽 大慶
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Redis緩存-序列化對象存儲亂碼問題的解決》,本文關(guān)鍵詞 Redis,緩存,序列化,對象,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。