我遇到了问题,我正在尝试在 Redis 中存储 UserSSE (我的自定义类)的对象。该对象包含内部数据、id 和 SseEmitter。我确实需要存储 SseEmitter 以供以后使用,我选择了 Redis,因为它将是一种快速获取和处理它的方法,因为我只会在短时间内使用此类对象几次。
如果我将 SseEmitter 更改为字符串(我只是出于调试目的而这样做),那么一切都会运行良好,因此问题肯定是在 Redis 中将此类 SseEmitter 存储为我的 UserSSE 的一部分。
这是我收到的错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 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 java.io.InvalidClassException: com.springbootsecurityjwt.demo.intercommunication.serverSideEvents.redis.entity.UserSSE; local class incompatible: stream classdesc serialVersionUID = -6802715826521416298, local class serialVersionUID = -2205037872029018309] with root cause
java.io.InvalidClassException: com.springbootsecurityjwt.demo.intercommunication.serverSideEvents.redis.entity.UserSSE; local class incompatible: stream classdesc serialVersionUID = -6802715826521416298, local class serialVersionUID = -2205037872029018309
这是 UserSSE 类:
@RedisHash("UserSSE")
public class UserSSE implements Serializable{
@Id
private Long id;
private SseEmitter sseEmitter;
*** Getters Setters and Constructors Skipped
}
这是将对象添加到 Redis 的 GET 端点:
@Autowired
private UserSSEDao userSSEDao;
@GetMapping("/memory")
public UserSSE handle(@AuthenticationPrincipal UserDetailsImpl userDetailsIml) {
SseEmitter emitter = new SseEmitter();
try{
UserSSE userSSE = new UserSSE();
userSSE.setId(userDetailsIml.getId());
userSSE.setSseEmitter(emitter);
userSSEDao.save(userSSE);
return userSSE;
}catch(Exception e){
e.printStackTrace();
}
return userSSEDao.findUserSSEById(userDetailsIml.getId());
}
您可以尝试创建扩展SseEmitter并实现Serialized接口的pojo
public class CustomSSE extends SseEmitter implements Serializable {
// super class override
}