Java 原因:org.springframework.data.redis.serializer.SerializationException:无法反序列化;嵌套的异常是org.springframework.core.seriali
我正在开发Spring Boot+Spring Data Redis示例。在本例中,尝试使用Java 原因:org.springframework.data.redis.serializer.SerializationException:无法反序列化;嵌套的异常是org.springframework.core.seriali,java,redis,jedis,spring-data-redis,Java,Redis,Jedis,Spring Data Redis,我正在开发Spring Boot+Spring Data Redis示例。在本例中,尝试使用@enableRedistpSession,并从中获取代码引用 我开发了代码,当我试图运行这个示例时。我得到下面的错误。我不知道发生了什么事?另外,我不清楚为什么错误是由:java.lang.ClassNotFoundException:com.example.Order引起的 Caused by: org.springframework.data.redis.serializer.Serializati
@enableRedistpSession
,并从中获取代码引用
我开发了代码,当我试图运行这个示例时。我得到下面的错误。我不知道发生了什么事?另外,我不清楚为什么错误是由:java.lang.ClassNotFoundException:com.example.Order
引起的
Caused by: org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is org.springframework.core.NestedIOException: Failed to deserialize object type; nested exception is java.lang.ClassNotFoundException: com.example.Order
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:84) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.serializer.DefaultRedisElementReader.read(DefaultRedisElementReader.java:48) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.read(RedisSerializationContext.java:226) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.cache.RedisCache.deserializeCacheValue(RedisCache.java:254) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.redis.cache.RedisCache.lookup(RedisCache.java:88) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.cache.support.AbstractValueAdaptingCache.get(AbstractValueAdaptingCache.java:58) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:73) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:554) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:519) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:401) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:345) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at com.example.service.OrderService$$EnhancerBySpringCGLIB$$2fc6ad4.byId(<generated>) ~[classes/:na]
at com.example.RedisApplication.lambda$4(RedisApplication.java:97) [classes/:na]
at com.example.RedisApplication.measure(RedisApplication.java:89) [classes/:na]
at com.example.RedisApplication.lambda$3(RedisApplication.java:98) [classes/:na]
at com.example.RedisApplication.lambda$0(RedisApplication.java:57) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
... 5 common frames omitted
Caused by: org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is org.springframework.core.NestedIOException: Failed to deserialize object type; nested exception is java.lang.ClassNotFoundException: com.example.Order
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:78) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:36) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:82) ~[spring-data-redis-2.1.2.RELEASE.jar:2.1.2.RELEASE]
... 24 common frames omitted
Caused by: org.springframework.core.NestedIOException: Failed to deserialize object type; nested exception is java.lang.ClassNotFoundException: com.example.Order
at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:75) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:73) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.example.Order
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_151]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_151]
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_151]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_151]
at java.lang.Class.forName0(Native Method) ~[na:1.8.0_151]
at java.lang.Class.forName(Unknown Source) ~[na:1.8.0_151]
at java.io.ObjectInputStream.resolveClass(Unknown Source) ~[na:1.8.0_151]
at org.springframework.core.ConfigurableObjectInputStream.resolveClass(ConfigurableObjectInputStream.java:80) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source) ~[na:1.8.0_151]
at java.io.ObjectInputStream.readClassDesc(Unknown Source) ~[na:1.8.0_151]
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) ~[na:1.8.0_151]
at java.io.ObjectInputStream.readObject0(Unknown Source) ~[na:1.8.0_151]
at java.io.ObjectInputStream.readObject(Unknown Source) ~[na:1.8.0_151]
at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:72) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 27 common frames omitted
Order.java
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("orders")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long Id;
@Indexed
private Date when;
@Reference
private List<LineItem> lineItems;
}
RedisApplication.java
@Log
@SpringBootApplication
@EnableRedisHttpSession
@EnableCaching
public class RedisApplication {
private final String TOPIC = "Chat";
@Autowired
private OrderRepository orderRepository;
@Autowired
private LineItemRepository lineItemRepository;
@Autowired
private OrderService OrderService;
private ApplicationRunner titleRunner(String title, ApplicationRunner rr) {
return args -> {
log.info(title.toUpperCase() + ":");
rr.run(args);
};
}
@Bean
ApplicationRunner geography(RedisTemplate<String, String> rt) {
return titleRunner("geography", args -> {
GeoOperations<String, String> geo = rt.opsForGeo();
geo.add("Sicily", new Point(13.361389, 38.155556), "Arigento");
geo.add("Ramesh", new Point(15.087269, 37.502669), "Catania");
geo.add("Anup", new Point(13.583333, 37.316667), "Palermo");
Circle circle = new Circle(new Point(13.583333, 37.316667),
new Distance(100, RedisGeoCommands.DistanceUnit.KILOMETERS));
GeoResults<GeoLocation<String>> radius = geo.radius("Sicily", circle);
radius.getContent().forEach(c -> log.info(c.toString()));
});
}
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
}
//#####################
@Bean
CacheManager redisCache(RedisConnectionFactory connectionFactory) {
return RedisCacheManager.builder(connectionFactory).build();
}
private long measure(Runnable r) {
long start = System.currentTimeMillis();
r.run();
long stop = System.currentTimeMillis();
return start - stop;
}
@Bean
ApplicationRunner cache() {
return titleRunner("caching", a ->{
Runnable measure = () -> OrderService.byId(1L);
log.info("first "+measure(measure));
log.info("two "+measure(measure));
log.info("three "+measure(measure));
});
}
//#####################
@Bean
ApplicationRunner repositories() {
return titleRunner("repositories", args -> {
Long orderId = generateId();
List<LineItem> itemsList = Arrays.asList(
new LineItem(orderId, generateId(), "plunger"),
new LineItem(orderId, generateId(), "soup"),
new LineItem(orderId, generateId(), "cofee mug"));
itemsList.stream().map(lineItemRepository::save).forEach(li -> log.info(li.toString()));
Order order = new Order(orderId, new Date(), itemsList);
orderRepository.save(order);
Collection<Order> found = orderRepository.findByWhen(order.getWhen());
found.forEach(o -> log.info("found : " + o.toString()));
});
}
private Long generateId() {
long tmp = new Random().nextLong();
return Math.max(tmp, tmp * -1);
}
@Bean
ApplicationRunner pubSub(RedisTemplate<String, String> rt) {
return titleRunner("publish/subscribe", args ->{
rt.convertAndSend(TOPIC, "Hello World @ "+Instant.now().toString());
});
}
@Bean
RedisMessageListenerContainer listener(RedisConnectionFactory rcf) {
MessageListener ml = (message, pattern) -> {
String str = new String(message.getBody());
log.info("message from ' " + TOPIC + "':'" + str);
};
RedisMessageListenerContainer mlc = new RedisMessageListenerContainer();
mlc.setConnectionFactory(rcf);
mlc.addMessageListener(ml, new PatternTopic(TOPIC));
return mlc;
}
}
@Log
@SpringBoot应用程序
@启用RedistpSession
@启用缓存
公共类应用程序{
私有最终字符串TOPIC=“Chat”;
@自动连线
专用医嘱存储库医嘱存储库;
@自动连线
私有LineItemRepository LineItemRepository;
@自动连线
私人订单服务;
专用ApplicationRunner标题运行程序(字符串标题,ApplicationRunner rr){
返回参数->{
log.info(title.toUpperCase()+“:”);
rr.run(args);
};
}
@豆子
ApplicationRunner地理位置(Redistrt模板){
返回标题Runner(“地理”,args->{
GeoOperations geo=rt.optsforgeo();
地理添加(“西西里岛”,新点(13.361389,38.155556),“阿里根托”);
地理添加(“Ramesh”,新点(15.087269,37.502669),“卡塔尼亚”);
地理添加(“Anup”,新点(13.583333,37.316667),“巴勒莫”);
圆=新圆(新点(13.583333,37.316667),
新距离(100,redieoCommands.DistanceUnit.km);
地理结果半径=地理半径(“西西里岛”,圆圈);
radius.getContent().forEach(c->log.info(c.toString());
});
}
公共静态void main(字符串[]args){
run(RedisApplication.class,args);
}
//#####################
@豆子
CacheManager redisCache(RedisConnectionFactory connectionFactory){
返回RedisCacheManager.builder(connectionFactory.build();
}
专用长度量(可运行的r){
长启动=System.currentTimeMillis();
r、 run();
长时间停止=System.currentTimeMillis();
返回启动-停止;
}
@豆子
ApplicationRunner缓存(){
返回标题Runner(“缓存”,a->{
可运行度量=()->OrderService.byId(1L);
日志信息(“第一个”+度量(度量));
日志信息(“两个”+度量(度量));
日志信息(“三”+度量(度量));
});
}
//#####################
@豆子
ApplicationRunner存储库(){
返回标题Runner(“存储库”,args->{
Long orderId=generateId();
List itemsList=Arrays.asList(
新的行项目(orderId,generateId(),“柱塞”),
新的行项目(orderId,generateId(),“soup”),
新的行项目(orderId,generateId(),“咖啡杯”);
itemsList.stream().map(lineItemRepository::save).forEach(li->log.info(li.toString());
订单=新订单(订单ID,新日期(),项目列表);
orderRepository.save(订单);
Collection found=orderRepository.findByWhen(order.getWhen());
found.forEach(o->log.info(“found:+o.toString()));
});
}
私有长生成ID(){
long tmp=new Random().nextLong();
返回数学最大值(tmp,tmp*-1);
}
@豆子
ApplicationRunner pubSub(重新发布模板){
返回标题Runner(“发布/订阅”,args->{
rt.convertAndSend(主题“Hello World@”+Instant.now().toString());
});
}
@豆子
RedisMessageListenerContainer侦听器(RedisConnectionFactory rcf){
MessageListener ml=(消息,模式)->{
String str=新字符串(message.getBody());
log.info(“来自“+TOPIC+”:“+str”的消息);
};
RedisMessageListenerContainer mlc=新的RedisMessageListenerContainer();
mlc.设置连接工厂(rcf);
addMessageListener(ml,新模式主题(TOPIC));
返回mlc;
}
}
我按照
@Arnaud
的建议,使用FLUSHALL冲洗Redis DB,然后工作正常。即使现在,我仍然可以看到SessionAttributes
和SessionExpire
等的详细信息被持久保存到缓存中
127.0.0.1:6379> KEYS *
1) "lineItems:3544713235909729939:idx"
2) "order-by-id::1"
3) "lineItems:orderId:4510658827505526003"
4) "Sicily"
5) "spring:session:expirations:1541668980000"
6) "lineItems:6802678421112514562"
7) "lineItems:4083773252419547322:idx"
8) "lineItems:4083773252419547322"
9) "lineItems:6802678421112514562:idx"
10) "spring:session:sessions:expires:ea0c3a35-b821-461b-8568-1cf972ba99cb"
11) "Anup"
12) "lineItems:3544713235909729939"
13) "orders:4510658827505526003:idx"
14) "orders:4510658827505526003"
15) "orders:when:1541667065759"
16) "Ramesh"
17) "lineItems"
18) "spring:session:sessions:ea0c3a35-b821-461b-8568-1cf972ba99cb"
19) "orders"
127.0.0.1:6379>
我想你可以尝试不同的serialVersion也许Redis包含一个旧的
com.example.Order
类中的序列化对象?请先尝试清除Redis数据库。谢谢刷新数据库后,它工作得很好。确切的问题是什么?可能您插入了具有com.example.Order
类的较旧版本应用程序的对象。ObjectInputStream无法将这些对象反序列化到另一个类(即使只有包名称不同)。您是对的。。非常感谢。你能给我引路吗
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("lineItems")
public class LineItem implements Serializable {
private static final long serialVersionUID = 1L;
@Indexed
private Long orderId;
@Id
private Long id;
private String description;
}
@Log
@SpringBootApplication
@EnableRedisHttpSession
@EnableCaching
public class RedisApplication {
private final String TOPIC = "Chat";
@Autowired
private OrderRepository orderRepository;
@Autowired
private LineItemRepository lineItemRepository;
@Autowired
private OrderService OrderService;
private ApplicationRunner titleRunner(String title, ApplicationRunner rr) {
return args -> {
log.info(title.toUpperCase() + ":");
rr.run(args);
};
}
@Bean
ApplicationRunner geography(RedisTemplate<String, String> rt) {
return titleRunner("geography", args -> {
GeoOperations<String, String> geo = rt.opsForGeo();
geo.add("Sicily", new Point(13.361389, 38.155556), "Arigento");
geo.add("Ramesh", new Point(15.087269, 37.502669), "Catania");
geo.add("Anup", new Point(13.583333, 37.316667), "Palermo");
Circle circle = new Circle(new Point(13.583333, 37.316667),
new Distance(100, RedisGeoCommands.DistanceUnit.KILOMETERS));
GeoResults<GeoLocation<String>> radius = geo.radius("Sicily", circle);
radius.getContent().forEach(c -> log.info(c.toString()));
});
}
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
}
//#####################
@Bean
CacheManager redisCache(RedisConnectionFactory connectionFactory) {
return RedisCacheManager.builder(connectionFactory).build();
}
private long measure(Runnable r) {
long start = System.currentTimeMillis();
r.run();
long stop = System.currentTimeMillis();
return start - stop;
}
@Bean
ApplicationRunner cache() {
return titleRunner("caching", a ->{
Runnable measure = () -> OrderService.byId(1L);
log.info("first "+measure(measure));
log.info("two "+measure(measure));
log.info("three "+measure(measure));
});
}
//#####################
@Bean
ApplicationRunner repositories() {
return titleRunner("repositories", args -> {
Long orderId = generateId();
List<LineItem> itemsList = Arrays.asList(
new LineItem(orderId, generateId(), "plunger"),
new LineItem(orderId, generateId(), "soup"),
new LineItem(orderId, generateId(), "cofee mug"));
itemsList.stream().map(lineItemRepository::save).forEach(li -> log.info(li.toString()));
Order order = new Order(orderId, new Date(), itemsList);
orderRepository.save(order);
Collection<Order> found = orderRepository.findByWhen(order.getWhen());
found.forEach(o -> log.info("found : " + o.toString()));
});
}
private Long generateId() {
long tmp = new Random().nextLong();
return Math.max(tmp, tmp * -1);
}
@Bean
ApplicationRunner pubSub(RedisTemplate<String, String> rt) {
return titleRunner("publish/subscribe", args ->{
rt.convertAndSend(TOPIC, "Hello World @ "+Instant.now().toString());
});
}
@Bean
RedisMessageListenerContainer listener(RedisConnectionFactory rcf) {
MessageListener ml = (message, pattern) -> {
String str = new String(message.getBody());
log.info("message from ' " + TOPIC + "':'" + str);
};
RedisMessageListenerContainer mlc = new RedisMessageListenerContainer();
mlc.setConnectionFactory(rcf);
mlc.addMessageListener(ml, new PatternTopic(TOPIC));
return mlc;
}
}
127.0.0.1:6379> KEYS *
1) "lineItems:3544713235909729939:idx"
2) "order-by-id::1"
3) "lineItems:orderId:4510658827505526003"
4) "Sicily"
5) "spring:session:expirations:1541668980000"
6) "lineItems:6802678421112514562"
7) "lineItems:4083773252419547322:idx"
8) "lineItems:4083773252419547322"
9) "lineItems:6802678421112514562:idx"
10) "spring:session:sessions:expires:ea0c3a35-b821-461b-8568-1cf972ba99cb"
11) "Anup"
12) "lineItems:3544713235909729939"
13) "orders:4510658827505526003:idx"
14) "orders:4510658827505526003"
15) "orders:when:1541667065759"
16) "Ramesh"
17) "lineItems"
18) "spring:session:sessions:ea0c3a35-b821-461b-8568-1cf972ba99cb"
19) "orders"
127.0.0.1:6379>