Tutorial for solving garbled characters in Redis connection pool storage objects in SpringBoot: Could not read JSON: Unexpected character (�) at position 0.

foreword

  SpringBoot is a very convenient Java development framework, but in the actual development process, we will inevitably encounter some problems. Here we will introduce the problem of garbled characters when we store objects in Redis when using SpringBoot, and will give a detailed solution.

problem background

  When using SpringBoot to connect to Redis, we usually choose to use RedisTemplate to operate. However, when using RedisTemplate to store a class in Redis, sometimes there will be garbled characters, as shown below:

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unexpected character (�) at position 0.

  This error message tells us that RedisTemplate cannot parse the object in JSON format, and an unexpected character (that is, garbled characters) appears at the first character position.

problem analysis

  After careful searching, we found that the reason for this problem is that RedisTemplate uses JdkSerializationRedisSerializer for serialization by default, and JdkSerializationRedisSerializer serializes objects into byte streams, which makes it impossible for us to parse normally after taking out the data.

solution

Solution 1: Replace the serializer

  To solve this problem, we can replace the serializer of RedisTemplate. In SpringBoot, we can configure by modifying the application.yml file:

spring:
  redis:
    host: localhost
    port: 6379
    password:
    timeout: 10000
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        max-wait: -1
        min-idle: 0
      shutdown-timeout: 3000
    # 更换序列化器
    keySerializer: org.springframework.data.redis.serializer.StringRedisSerializer
    valueSerializer: org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer

  Here we replace both keySerializer and valueSerializer with StringRedisSerializer and GenericJackson2JsonRedisSerializer.

Solution 2: Modify the storage method

  In addition to changing the way of serializers, we can also choose to modify the way of storage. For example, instead of storing objects directly in Redis, convert them into JSON format and store them. The sample code is as follows:

@Autowired
private RedisTemplate redisTemplate;

public void setObject(String key, Object value) {
    
    
    // 将对象转换成JSON字符串
    String jsonString = JSON.toJSONString(value);
    redisTemplate.opsForValue().set(key, jsonString);
}
    
public Object getObject(String key) {
    
    
    String value = (String) redisTemplate.opsForValue().get(key);
    // 将JSON字符串转换成对象
    return JSON.parseObject(value, Object.class);
}

  In this way, we can correctly store objects in Redis and take them out smoothly when needed.

conclusion

  The above is this tutorial on solving the garbled problem of Redis connection pool storage objects in SpringBoot. I hope it can help you when you encounter similar problems in actual development.

Guess you like

Origin blog.csdn.net/java_cpp_/article/details/130798665