Spring数据redis覆盖默认序列化程序

时间:2018-05-10 11:41:35

标签: spring-boot redis spring-data spring-data-redis

我正在尝试创建一个RedisTemplate bean,它将使用更新的值序列化程序在redis中以JSON格式序列化对象。

@Configuration
class RedisConfig {

  @Bean(name = ["redisTemplate"])
  @Primary
  fun template(factory: RedisConnectionFactory): RedisTemplate<Any, Any> {
    val template = RedisTemplate<Any, Any>()
    template.connectionFactory = factory
    template.valueSerializer = Jackson2JsonRedisSerializer(Object::class.java)
    template.afterPropertiesSet()
    return template
  }
}

根据我的理解,spring应该使用JSON序列化程序来序列化由Cacheable注释标记的方法返回的对象。尽管有这样的配置,spring似乎正在使用默认的Java序列化程序,因为这个例外证实了这一事实。

java.io.NotSerializableException: en.prateekj.vds.dto.Task
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at java.util.ArrayList.writeObject(ArrayList.java:766)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1128)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:46)
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:63)
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:35)
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.serialize(JdkSerializationRedisSerializer.java:94)
at org.springframework.data.redis.serializer.DefaultRedisElementWriter.write(DefaultRedisElementWriter.java:43)
at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.write(RedisSerializationContext.java:219)
at org.springframework.data.redis.cache.RedisCache.serializeCacheValue(RedisCache.java:238)
at org.springframework.data.redis.cache.RedisCache.put(RedisCache.java:144)
at org.springframework.cache.interceptor.AbstractCacheInvoker.doPut(AbstractCacheInvoker.java:87)
at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:770)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:398)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:314)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)

我是否遗漏了弹簧无法确定要使用RedisTemplate的任何配置或其他内容?

1 个答案:

答案 0 :(得分:4)

您可能已同时解决了这个问题,但对于寻求答案的人来说。

根据spring数据redis reference

  

默认情况下,RedisCache和RedisTemplate配置为使用Java本机序列化。

从stacktrace中可以看到您实际上是在使用Redis进行缓存,因此您需要配置RedisCache而不是RedisTemplateRedisCache没有拿起您的@Bean,因为它没有在内部使用RedisTemplate

示例如何使用Java做到这一点:

    @EnableCaching
    @Configuration
    public class CacheConfig {

        @Bean
        @Primary
        public RedisCacheConfiguration defaultCacheConfig(ObjectMapper objectMapper) {
            return RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)));
        }

    }