Getting error : Could not write JSON: Java 8 date/time type `java.time.LocalDateTime` not supported by default

111 views Asked by At

I am getting this error while I am trying to saving my jsonObject to redis : Could not write JSON: Java 8 date/time type java.time.LocalDateTime not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling.

As a solution I have added this module in my pom. xml and also using object mapper added the module but still I am getting this error.

1

There are 1 answers

0
rodpold On

If you can't deserialize LocalDate or LocalDatetime when get from redis cache. this caused by jackson-datatype-jsr310. However, even if this module is included, you will still get a warning.

In my case: find my redis cache config. And I'll found this default cache config.

private RedisCacheConfiguration defaultCacheConfig() {
    return RedisCacheConfiguration.defaultCacheConfig()
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
            .entryTtl(Duration.ofSeconds(120)); // default
}

and GenericJackson2JsonRedisSerializer() used ObjectMapper.

so i changed my code like this:

private RedisCacheConfiguration defaultCacheConfig() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    return RedisCacheConfiguration.defaultCacheConfig()
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)))
            .entryTtl(Duration.ofSeconds(120)); // default
}

so this JavaTimeModule() has 'jackson-datatype-jsr310' and my case solved.