I saved a json string of an object for each key in redis, but there were some strange characters when I took it out
this.redisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(XXX), 15 * 1000);
This is the value code:
String xxxJSON= this.redisTemplate.opsForValue().get(key);
XXX xxx = objectMapper.readValue(xxxJSON, XXX.class);
This is the output:
redis取到的值==== NULNULNULNULNULNULNULNULNUL。。。。。{json 内容}
This is what you see in redis manager
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00。。。。。。。。。。。。还有很多{json数据}
The previous NUL log output can be seen, but when copied, it is filled with spaces. In order to let everyone see the effect, the handwritten NUL
The main problem is that I need to convert the retrieved data into an object. If I remove the NUL in front of it, it can be converted into an object. If I add it, an error will be reported, but the copied data will be filled with spaces.
Other data Fortunately, there is no problem with set list or string. Only json string has this problem.
Has anyone encountered this problem? The main reason is that they don’t understand why this problem occurs. JSON strings are also strings. Why don’t ordinary strings have this problem? JSON strings have this problem.
Also try to use s to match space replacement and trim to remove the leading and trailing spaces, but it has no effect and the string is not transformed
trim can be used after removing the spaces. It was not the latest code when testing, and "s" cannot be matched. I don't know what happened. In addition, the Java Date object is stored in redis and automatically converted into a timestamp. The converted timestamp is less than the real time. It took me an hour and I still couldn’t find the reason. What a trap
Using StringRedistemplate
Try using StringRedisTemplate instead of RedisTemplate