Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of garbled data written by Spring-RedisTemplate

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to solve the garbled code when Spring-RedisTemplate writes data". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Problem recurrence project depends on org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-data-redisRedis configuration

Yaml file configuration

Spring: application: name: booklet-redis redis: host: 127.0.0.1 port: 6379 password: adminadmin timeout: 5000ms

Redis configuration class

Package com.liumapp.booklet.redis.config;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.*;import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configurationpublic class RedisConfig {/ * inject RedisConnectionFactory * / @ Autowired RedisConnectionFactory redisConnectionFactory @ Bean public RedisTemplate functionDomainRedisTemplate () {RedisTemplate redisTemplate = new RedisTemplate (); initDomainRedisTemplate (redisTemplate, redisConnectionFactory); return redisTemplate;} / * set the serialization method for storing data in redis * * @ param redisTemplate * @ param factory * / private void initDomainRedisTemplate (RedisTemplate redisTemplate, RedisConnectionFactory factory) {redisTemplate.setKeySerializer (new StringRedisSerializer ()) RedisTemplate.setHashKeySerializer (new StringRedisSerializer ()); redisTemplate.setHashValueSerializer (new JdkSerializationRedisSerializer ()); redisTemplate.setValueSerializer (new JdkSerializationRedisSerializer ()); redisTemplate.setConnectionFactory (factory);} / * instantiate HashOperations objects. You can use the Hash type operation * * @ param redisTemplate * @ return * / @ Bean public HashOperations hashOperations (RedisTemplate redisTemplate) {return redisTemplate.opsForHash () } / * instantiate ValueOperations objects. You can use the String operation * * @ param redisTemplate * @ return * / @ Bean public ValueOperations valueOperations (RedisTemplate redisTemplate) {return redisTemplate.opsForValue () } / * instantiate ListOperations objects. You can use the List operation * * @ param redisTemplate * @ return * / @ Bean public ListOperations listOperations (RedisTemplate redisTemplate) {return redisTemplate.opsForList () } / * instantiate SetOperations objects. You can use the Set operation * * @ param redisTemplate * @ return * / @ Bean public SetOperations setOperations (RedisTemplate redisTemplate) {return redisTemplate.opsForSet () Instantiate the ZSetOperations object using the ZSet operation * * @ param redisTemplate * @ return * / @ Bean public ZSetOperations zSetOperations (RedisTemplate redisTemplate) {return redisTemplate.opsForZSet ();}} to test the code package com.liumapp.booklet.redis;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest Import org.springframework.data.redis.core.ListOperations;import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;import java.util.ArrayList;import java.util.List;@SpringBootTest (classes = BookletRedisMain.class) @ RunWith (SpringRunner.class) public class BookletRedisMainTest {@ Resource private ListOperations listOperations; @ Test public void leftPushTest () {List list = new ArrayList (); list.add ("hello world"); listOperations.leftPush ("listKey", list) }}

After running the above test code, we insert a set of data of type list into redis, whose key is listKey,value and a list object with only one element.

Next, we use redis-cli to get the value of listKey, and we can see the occurrence of garbled codes:

127.0.0.1 xed 6379 > LRANGE listKey 0101) "\ xac\ xed\ X00\ x05sr\ X00\ x13java.util.ArrayListx\ x81\ xd2\ x1d\ X99\ xc7a\ x9d\ x03\ X00\ x04sizexp\ X00\ X00\ x01w\ X04\ X00\ X00\ X01t\ X00\ x0bhello worldx"

Of course, this has no effect on the actual use of our project, and there will be no garbled code when we get listKey again in the program. Only when we get the value directly through tools such as redis-cli will there be garbled code.

The cause of the problem

The reason for the problem is the code we configured for Redis (in fact, this is also the default configuration code for redisTemplate):

Private void initDomainRedisTemplate (RedisTemplate redisTemplate, RedisConnectionFactory factory) {redisTemplate.setKeySerializer (new StringRedisSerializer ()); redisTemplate.setHashKeySerializer (new StringRedisSerializer ()); redisTemplate.setHashValueSerializer (new JdkSerializationRedisSerializer ()); redisTemplate.setValueSerializer (new JdkSerializationRedisSerializer ()); redisTemplate.setConnectionFactory (factory);}

Here, redisTemplate uses the default serialization policy of JDK for the serialization classes of HashValue and Value, rather than the serialization policy of String type, so the value we see in redis-cli will have garbled code because of the serialization policy problem.

Solution.

Change JDK default serialization policy to String type serialization policy

RedisTemplate.setHashValueSerializer (new StringRedisSerializer ()); redisTemplate.setValueSerializer (new StringRedisSerializer ())

But when we do this, we can only store data of type String, so the test code needs to be modified as follows

@ Testpublic void leftPushTest () {List list = new ArrayList (); list.add ("hello world2"); listOperations.leftPush ("listKey", list.toString ());}

Once again, take the value from redis-cli and get the following result:

127.0.0.1 xac 6379 > LRANGE listKey 0 101) "[hello world2]" 2) "\ xac\ xed\ X00\ x05sr\ X00\ x13java.util.ArrayListx\ x81\ xd2\ x99\ xc7a\ x9d\ X03\ x00\ x01I\ X00\ x04sizexp\ X00\ X01w\ X04\ X00\ X00\ X01t\ X00\ x0bhello worldx"

It can be found that the garbled problem has been solved.

Summary

It is not recommended to change the default serialization policy of redisTemplate. If there is garbled code, let it be messed up. Anyway, knowing the correct decoding strategy will not affect the normal operation of the program (although it seems difficult to obtain the value of redis through other languages such as php).

This is the end of the content of "how to solve the garbled Spring-RedisTemplate data". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report