Spring data redis 覆盖默认序列化器 [英] Spring data redis override default serializer
问题描述
我正在尝试创建一个 RedisTemplate
bean,该 bean 将具有更新的值序列化程序以在 redis 中以 JSON
格式序列化对象.
I am trying to create a RedisTemplate
bean which will have the updated value serializer to serialize an object in JSON
format in redis.
@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 序列化程序,因为这个异常证实了这一事实.
As per my understanding, spring should use the JSON serializer to serialize the object returned by the methods marked with Cacheable
annotation. Despite this configuration, spring seems to be using the default Java serializer as this exception confirms this fact.
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)
我是否缺少任何配置或 spring 无法确定要使用的 RedisTemplate
的任何配置?
Am I missing any configuration or something by which spring is not able to determine what RedisTemplate
to use?
推荐答案
您可能同时解决了它,但对于进一步的答案寻求者.
You have probably solved it meanwhile, but for further answer seekers.
根据spring数据redis 参考:
According to spring data redis reference:
默认情况下,RedisCache 和 RedisTemplate 配置为使用 Java 原生序列化.
By default, RedisCache and RedisTemplate are configured to use Java native serialization.
从堆栈跟踪中我可以看到您实际上是在使用 Redis 进行缓存,因此您需要配置 RedisCache
而不是 RedisTemplate
.RedisCache
没有接收您的 @Bean
,因为它没有在内部使用 RedisTemplate
.
From stacktrace I can see that you are actually using Redis for caching, so you need to configure RedisCache
and not RedisTemplate
. RedisCache
is not picking up your @Bean
because it is not using RedisTemplate
internally.
示例如何在 Java 中执行此操作:
Example how you can do it in 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)));
}
}
这篇关于Spring data redis 覆盖默认序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!