Skip to content
Advertisement

Spring Data with Redis: How do I use a different LocalDateTime format or a different convertor?

I have data in my DB for field date with the following format: 2021-09-21 11:25:36. The Redis field is of type TEXT.

When I’m trying to read the data from date field from the DB, I get following exception:

Failed to convert from type [byte[]] to type [java.time.LocalDateTime] for value '{50, 48, 50, 49, 45, 48, 57, 45, 50, 49, 32, 49, 49, 58, 50, 53, 58, 51, 54}'; nested exception is java.time.format.DateTimeParseException: Text '2021-09-21 11:25:36' could not be parsed at index 10
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [byte[]] to type [java.time.LocalDateTime] for value '{50, 48, 50, 49, 45, 48, 57, 45, 50, 49, 32, 49, 49, 58, 50, 53, 58, 51, 54}'; nested exception is java.time.format.DateTimeParseException: Text '2021-09-21 11:25:36' could not be parsed at index 10
(...)
Caused by: java.time.format.DateTimeParseException: Text '2021-09-21 11:25:36' could not be parsed at index 10
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2050)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:493)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:478)
    at org.springframework.data.redis.core.convert.Jsr310Converters$BytesToLocalDateTimeConverter.convert(Jsr310Converters.java:113)
    at org.springframework.data.redis.core.convert.Jsr310Converters$BytesToLocalDateTimeConverter.convert(Jsr310Converters.java:108)
    at org.springframework.core.convert.support.GenericConversionService$ConverterAdapter.convert(GenericConversionService.java:386)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
    ... 142 more

How can I assign a different convertor to this field in my entity or annotate that my LocalDateTime format is different than what is being expected? My current assumption is that the issue resides in the missing ‘T’ between the date and the time, but I have little to no possibility to change the data from the DB.

Advertisement

Answer

I found the answer a bit later myself using RedisCustomConverters:

This bean is necessary:

    @Bean
    public RedisCustomConversions redisCustomConversions(BytesToLocalDateTimeConverter bytesToLocalDateTimeConverter) {
        return new RedisCustomConversions(List.of(bytesToLocalDateTimeConverter));
    }

in combination with this custom converter:

@Component
@ReadingConverter
public class BytesToLocalDateTimeConverter implements Converter<byte[], LocalDateTime> {

    @Override
    public LocalDateTime convert(final byte[] source) {
        return LocalDateTime.parse(new String(source), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement