spring boot redis kv garbled

The default serialization of redisTemplate is jdkSerializeable. This method will lead to garbled characters such as \xAC\xED\x00\x05t\x00\x011 for key/value.

will cause two problems

a. not easy to read

b. Unable to interact with other languages

The following is the solution process


1. Add the config class to configure the default serialization mode for redisTemplate

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //Set the instantiation object of serialized Value
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        redisTemplate.setHashValueSerializer(new StringRedisSerializer());

After the test, operations such as setkey lpush are no longer garbled, but the original function of directly writing the Java obj list cannot be used.

2. Comment on the second sentence above

//redisTemplate.setValueSerializer(new StringRedisSerializer());

Restore value to the default serialization mode

Then call in a small number of places that need to deal with other languages

redisTemplate.setValueSerializer(new StringRedisSerializer());

dosomethingwithredis()

 redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());

Finally restore it.







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325954562&siteId=291194637