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:
- [Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`
- Springboot integrates Redis annotation and access error: java.io.NotSerializableException: com.demo.entity.MemberEntity
- Defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configuration.
- [Solved] Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configu
- Java back end receives localdatetime type parameter
- [Solved] java.lang.IllegalAccessError: class org.springframework.data.redis.core.$ Proxy237 cannot access its superinterface org.springframework.data.redis.core.RedisConnectionUtils$RedisConnectionProxy
- The java springboot websocket service server actively closes the connection and causes java.io.EOFException to be thrown
- Springboot startup error: err config is disabled command (Redis Disables Config command)
- JAVA: How to Solve Foreach Loop Remove/add Element Error
- [Solved] IDEA springboot Startup Error: java.lang.UnsatisfiedLinkError: no tcnative-1 in java.library.path
- [Solved] Redis Error: org.springframework.data.redis.RedisConnectionFailureExceptionjava.net.SocketTimeoutException
- JAVA Connect Redis Error: stop-writes-on-bgsave-error option
- Redis: DENIED Redis is running in protected mode [How to Solve]
- [Solved] Springboot project introduces Font library error: java.awt.fontformatexception: bad table, tag = XXXXXX
- Redis: How to Implementate LRU Caching Mechanism Manually
- [Solved] IDEA Add maven Project Error: Error:(3,21)java: Package javax.servletdoes not exist
- [Solved] Project Startup Error: Redis health check failed:Unable to connect to localhost6379
- [Solved] Project Startup Error: Redis health check failed: Unable to connect to localhost6379
- Failed to restart redis-server.service Unit not found [How to Solve]
- [Solved] Springboot2.x ElasticSearch Error: availableProcessors is already set to [4], rejecting [4]