I use spring Data Redis Reactive framework and spring Boot version is 2.3.0.
Here is my Redis configuration:
@Bean("reactiveRedisTemplate")
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplateString(ReactiveRedisConnectionFactory connectionFactory) {
Jackson2JsonRedisSerializer jacksonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
JavaTimeModule timeModule = new JavaTimeModule();
timeModule.addSerializer(Date.class, new DateSerializer(Boolean.FALSE, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
objectMapper.registerModule(timeModule).registerModule(new ParameterNamesModule()).registerModules(ObjectMapper.findModules());
jacksonSerializer.setObjectMapper(objectMapper);
final StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
RedisSerializationContext<String , Object> serializationContext = RedisSerializationContext
.<String, Object>newSerializationContext()
.key(stringRedisSerializer)
.value(jacksonSerializer)
.hashKey(stringRedisSerializer)
.hashValue(jacksonSerializer)
.build();
return new ReactiveRedisTemplate(connectionFactory, serializationContext);
}
I set up a LocalDateTime object with the hashoperations.put method to display the following form in Redis:
key hashKey "2020-06-23 16:42:44"
When I use Mono<LocalDateTime>Mono = hashoperations.get(key,hashKey) to get the value, the following exception occurs:
java.lang.ClassCastException: class java.lang.String cannot be cast to class java.time.LocalDateTime (java.lang.String and java.time.LocalDateTime are in module java.base of loader 'bootstrap')
at reactor.core.publisher.FluxFilter$FilterSubscriber.onNext(FluxFilter.java:93)
at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:76)
But when LocalDateTime is a property of an object, there is no problem.
I don't know how to solve it. Thank you for all the answers.
I've solved it
change DefaultTyping.NON_FINAL to DefaultTyping.EVERYTHING
This will take the type of java.time.LocalDateTime