Spring Boot project, Jackson serialization and deserialization RedisTemplete code reference (detailed explanation)

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    
    
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    
    
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(factory);
    Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    
    ObjectMapper objectMapper = new ObjectMapper();
    // 设置 ObjectMapper 的可见性,将所有属性设置为可见性,包括私有属性。
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    // 配置 ObjectMapper,在遇到未知属性时不会抛出异常。
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    
    // 激活 ObjectMapper 的默认类型信息,将类型信息作为属性添加到 JSON 字符串中。
    objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
            ObjectMapper.DefaultTyping.EVERYTHING,
            JsonTypeInfo.As.PROPERTY);

    // objectMapper.deactivateDefaultTyping(); 这个配置不会添加全类名
    
    // 配置好的 ObjectMapper 设置到 jsonRedisSerializer 中。
    jsonRedisSerializer.setObjectMapper(objectMapper);
    
    // 设置 RedisTemplate 的值序列化器为 jsonRedisSerializer。
    redisTemplate.setValueSerializer(jsonRedisSerializer);
    // 设置 RedisTemplate 的键序列化器为 StringRedisSerializer。
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    
    // 设置 RedisTemplate 的哈希键序列化器为 StringRedisSerializer。
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    // 设置 RedisTemplate 的哈希值序列化器为 jsonRedisSerializer。
    redisTemplate.setHashValueSerializer(jsonRedisSerializer);
    // 对 RedisTemplate 进行必要的初始化。
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
}

}

Supplementary Note

These few lines of code will add the full class name to the serialized Json string

objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
            ObjectMapper.DefaultTyping.EVERYTHING,
            JsonTypeInfo.As.PROPERTY);

Please add a picture description
If configured like this, the full class name will not be added

objectMapper.deactivateDefaultTyping();  // 这个配置不会添加全类名

insert image description here

Benefits of adding fully qualified class names

Adding full class name information to the serialized JSON string can bring the following effects:

  1. Type Safety: Adding fully qualified class name information ensures that JSON strings are correctly converted back to the corresponding objects when deserializing. Type information helps the deserializer identify and instantiate the correct object type, avoiding data loss or type conversion errors.

  2. Polymorphism support: By adding full class name information, the polymorphism of objects can be restored when deserializing. Even if the serialized object is an instance of a concrete subclass, it will correctly revert to an object of that subclass when deserialized, not just the base class or interface type.

  3. Version Compatibility: Class name information is useful for handling version migration and compatibility. When you modify the class name or class structure of an object, the deserializer can use the class name information to adapt to the new class structure without causing deserialization failure or data loss.

It should be noted that adding the full class name information to the JSON string may increase the length of the serialized string, increase storage space and network transmission costs. Therefore, when deciding whether to add full class name information, the specific needs and performance requirements of the application need to be considered.

Guess you like

Origin blog.csdn.net/qq_45796667/article/details/130837242