使用Spring ,我序列化一个对象并将其存储到Redis中。序列化策略是Jackson2JsonRedisSerializer。因此,假设我的bean是Sample.java,下面的prop1, prop2属性是如何添加到缓存中的
\xAC\xED\x00\x05sr\x00'com.main.model.Sample\x90\x91\xFB4\xDD\x9D\xE1\xBB\x02\x00\x11J\x00\x0Bprop1J\x00\x0Aprop2J正如我们所看到的那样,对象类型info也是以其完全限定的名称存储的。
现在有多个服务可以通过反序列化和更新这个条目,将其写回缓存。(各种服务中的模型/bean有不同的完全限定名)
当不同的服务试图用ClassNotFoundException反序列化其失败时,就会出现此问题。
org.springframework.data.redis.serializer.SerializationException:
Cannot serialize; nested exception is
org.springframework.core.serializer.support.SerializationFailedException:
Failed to deserialize object type;
nested exception is java.lang.ClassNotFoundException: com.xyz.model.BasicSample这是一个样本
服务1
@Override
@Cacheable(value = "sample", key = "{ #sample.sampleId }", unless = "#result == null")
public Sample fetchSample(Sample sample) {...}服务2
@Override
@Cacheable(value = "sample", key = "{ #sample.sampleId }", unless = "#result == null")
public BasicSample fetchBasicSample(BasicSample sample) {...}有没有办法
发布于 2020-02-02 03:42:41
我刚刚花了半天时间在同一个问题上,所以把答案留给子孙后代:
通常,Spring将使用_class键的完整类名,但是开发人员可以使用@TypeAlias("someAlias")注释对其进行自定义。这将覆盖_class字段。
因此,可以将这两个类定义为:
package com.example.service1;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
@RedisHash(value = "sample")
@TypeAlias("SampleType")
public class Sample {
@Indexed
int id;
String name;
...
}package com.example.service2;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
@RedisHash(value = "sample")
@TypeAlias("SampleType")
public class Sample {
@Indexed
int id;
String name;
...
}现在,redis对象将独立于类包名称进行反序列化。
https://stackoverflow.com/questions/59610666
复制相似问题