Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

536
Vistas
Redis - How to configure custom conversions

In spring-data-redis, How do we need configure custom converters that can be auto-wired/injected from Spring boot application or configuration.

I read about @ReadingConverter and @WritingConverter from spring data redis documentation. From this documentation, it is not clear on how to configure them. https://github.com/spring-projects/spring-data-redis/blob/master/src/main/asciidoc/reference/redis-repositories.adoc#redis.repositories.indexes

Does anyone know how to do it?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Tested with spring-boot-starter-data-redis:2.0.4.RELEASE.

I was facing a problem where my OffsetDateTime properties of my @RedisHash entity were not being stored when using CrudRepository.

The problem was that Jsr310Converters does not have a converter of OffsetDateTime.

To solve this, I created a reading converter:

@Component
@ReadingConverter
public class BytesToOffsetDateTimeConverter implements Converter<byte[], OffsetDateTime> {
    @Override
    public OffsetDateTime convert(final byte[] source) {
        return OffsetDateTime.parse(new String(source), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    }
}

and writing converter:

@Component
@WritingConverter
public class OffsetDateTimeToBytesConverter implements Converter<OffsetDateTime, byte[]> {
    @Override
    public byte[] convert(final OffsetDateTime source) {
        return source.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME).getBytes();
    }
}

And registered a RedisCustomConversions bean in the configuration:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.convert.RedisCustomConversions;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

import java.util.Arrays;

@Configuration
@EnableRedisRepositories
public class RedisConfiguration {

    @Bean
    public RedisCustomConversions redisCustomConversions(OffsetDateTimeToBytesConverter offsetToBytes,
                                                         BytesToOffsetDateTimeConverter bytesToOffset) {
        return new RedisCustomConversions(Arrays.asList(offsetToBytes, bytesToOffset));
    }

}
about 4 years ago · Santiago Trujillo Denunciar

0

You have to declare CustomConversions bean named "redisCustomConversions" in your application configuration.

@Bean
public CustomConversions redisCustomConversions(){
    return new CustomConversions(Arrays.asList(new YourWritingConverter(), new YourReadingConverter()));
}
about 4 years ago · Santiago Trujillo Denunciar

0

These code may help anyone. Thanks @Mikhail

@Component
public class RedisObjectHelper {

    @Resource
    private RedisTemplate<String, ?> redisTemplate;
    private HashOperations<String, byte[], byte[]> hashOperations;
    private HashMapper<Object, byte[], byte[]> mapper;

    @PostConstruct
    public void init() {
        mapper = new ObjectHashMapper(new CustomConversions(Arrays.asList(new Timestamp2ByteConverter(), new Byte2TimestampConverter())));
        hashOperations = redisTemplate.opsForHash();
    }
    // and any methods
}

tested with spring-data-redis-1.8.4.RELEASE

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda