Example usage for org.springframework.data.redis.serializer RedisSerializer RedisSerializer

List of usage examples for org.springframework.data.redis.serializer RedisSerializer RedisSerializer

Introduction

In this page you can find the example usage for org.springframework.data.redis.serializer RedisSerializer RedisSerializer.

Prototype

RedisSerializer

Source Link

Usage

From source file:am.ik.categolj3.api.entry.redis.EntryRedisTemplateFactory.java

public RedisTemplate<Object, Object> create() {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    template.setKeySerializer(new JdkSerializationRedisSerializer());
    template.setValueSerializer(new RedisSerializer<Entry>() {
        @Override/*  w w  w.  j  av a2 s.co m*/
        public byte[] serialize(Entry entry) throws SerializationException {
            if (entry == null) {
                return new byte[0];
            }
            try {
                return objectMapper.writeValueAsBytes(entry);
            } catch (JsonProcessingException e) {
                throw new SerializationException("Cannot serialize " + entry, e);
            }
        }

        @Override
        public Entry deserialize(byte[] bytes) throws SerializationException {
            if (bytes == null || bytes.length == 0) {
                return null;
            }
            try {
                return objectMapper.readValue(bytes, Entry.class);
            } catch (IOException e) {
                throw new SerializationException("Cannot deserialize " + Arrays.toString(bytes), e);
            }
        }
    });
    return template;
}