[Solved] Springboot uses redis to add LocaldateTime Java 8 error

To store an object in redis, you need to serialize the object. If a field is of localdatetime type, an error will appear

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: com.gd.base.vo.redis.RedisSysUser["Corresponding fields"])

The program needs to deserialize the data in redis. The deserializer I use here is the following:

@Configuration
@EnableCaching//Allow us to use the cache
public class RedisConfig {
    /**
     * Cache expiration time (seconds)
     */
    public static final long CACHE_EXPIRE_SECEND = 3600 * 2;

    @Bean // At this point, load our redisTemplate into the context of our spring, applicationContext
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        //1. Initialize a redisTemplate
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<String,Object>();
        //2. Serial words (generally used for key values)
        RedisSerializer<String> redisSerializer=new StringRedisSerializer();
        //3. Introduce the json string conversion class (generally used for value processing)
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper=new ObjectMapper();
        //3.1 Set the access rights of objectMapper
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //3.2 Specify the serialized input type, that is, store the data in the database to the redis cache according to certain types.
        </objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);// Recently upgraded SpringBoot, found that enableDefaultTyping method expired. You can use the following method instead
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As. WRAPPER_ARRAY);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        //4. Create the link
        redisTemplate.setConnectionFactory(factory);
        //4.1 redis key value serialization
        redisTemplate.setKeySerializer(redisSerializer);
        //4.2 value serialization, because most of our values are converted through objects, so use jackson2JsonRedisSerializer
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // 4.3 Serialization of value, serialization of hashmap
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory){
        // 1. Serial words (generally used for key values)
        RedisSerializer<String> redisSerializer=new StringRedisSerializer();
        //2. Introduce the json string conversion class (generally used for value processing)
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper=new ObjectMapper();
        //2.1 Set the access rights of objectMapper
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //2.2 Specify the serialized input type, that is, store the data in the database to the redis cache according to a certain type.
        </objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);// Recently upgraded SpringBoot, found that enableDefaultTyping method expired. You can use the following method instead
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As. WRAPPER_ARRAY);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        //3. Serialization configuration, garbled problem solving and timeliness of our cache
        RedisCacheConfiguration config=RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofSeconds(CACHE_EXPIRE_SECEND)). // Cache timeliness setting
                serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)). //key serialization
                serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)). //value serialization
                disableCachingNullValues();//null values are not stored in the cache
        //4. Create the cacheManager link and set the properties
        RedisCacheManager cacheManager= RedisCacheManager.builder(factory).cacheDefaults(config).build();
        return cacheManager;
    }

}

Processing error reports:
① add comments to the corresponding fields

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)

The error report is gone

Read More: