spring-data-redis - About spring data redis storage object json string, some unexpected characters appear in the value value
PHP中文网
PHP中文网 2017-05-31 10:31:22
0
2
1076

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

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
大家讲道理

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template