spring-data-redis介面調用出現亂碼的問題

來源:互聯網
上載者:User

先上個圖,使用redisTemplate介面調用資料的時候發現結果是空的,這個就奇怪了,寫進去的資料用可視化軟體查出來是這樣的:

ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext-redis2.xml");redisTemplate = (RedisTemplate)classPathXmlApplicationContext.getBean("redisTemplate");
public void testGeoPos(){    ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();    Set set = zSetOperations.range("beijing",0,zSetOperations.size("beijing"));    System.out.print(set.toString()); //   zSetOperations.add("beijing","hehe",1);    GeoOperations<String, String> geoOperations = redisTemplate.opsForGeo();    List<Point> list = redisTemplate.opsForGeo().geoPos("beijing","dongcheng");    System.out.println(list.toString());}
用終端登陸進去以後探索資料確實是建立了,但是名稱前面多了亂碼


解決的方法,百度以後發現通過網路傳輸的資料必須序列化以後才能使用,原因可以百度一下,有兩種解決辦法:

1、在代碼裡將redisTemplate序列化;

2、spring架構可以在XML裡配置序列化。

redisTemplate.setKeySerializer(stringSerializer);redisTemplate.setValueSerializer(stringSerializer);redisTemplate.setHashKeySerializer(stringSerializer);redisTemplate.setHashValueSerializer(stringSerializer);

 <bean id="redisTemplate" name="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">        <property name="connectionFactory" ref="redisConnectionFactory" />        <property name="keySerializer" ref="stringRedisSerializer" />        <property name="valueSerializer" ref="stringRedisSerializer" />        <property name="hashKeySerializer" ref="stringRedisSerializer" />        <property name="hashValueSerializer" ref="jackson2JsonRedisSerializer" />        <!-- <property name="enableTransactionSupport" value="true"/> -->    </bean>
 <bean id="stringRedisSerializer"        class="org.springframework.data.redis.serializer.StringRedisSerializer" />

 

修改以後就可以正確讀取了



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.