简体   繁体   English

Kryo反序列化时无法加载class异常

[英]Unable to load class exception during Kryo deserialization

I am using Kryo for serialization / deserialization and not registering classes beforehand (I am working on that).我正在使用 Kryo 进行序列化/反序列化,而不是事先注册类(我正在努力)。 That said, upon deserialization, I am getting the exception:也就是说,在反序列化时,我得到了异常:

Unable to load class shell.api.model.BatteryStatuo with kryo's ClassLoader.无法使用 kryo 的类加载器加载 class shell.api.model.BatteryStatuo。 Retrying with current..重试当前..

Now, my classname is actually shell.api.model.BatteryStatus so I'm not sure what happened during serialization.现在,我的类名实际上是 shell.api.model.BatteryStatus 所以我不确定在序列化过程中发生了什么。

Is there a limitation on the length of the classname?类名的长度有限制吗?

Also, as I am serializing JPA entities which have nested structures and likely have circular references, will that pose a potential issue?另外,当我序列化 JPA 具有嵌套结构并可能具有循环引用的实体时,这会带来潜在的问题吗? I would think I'd see a stack overflow exception if so.如果是这样,我想我会看到堆栈溢出异常。

This is a snippet of serializing an object:这是序列化 object 的片段:

protected final Kryo kryo = new Kryo();


try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
    try (final Output output = new Output(baos)) {
          kryo.writeObject(output, data);
        }
      return (baos.toByteArray());
    } catch (IOException e) {
      LOGGER.error("error serializing", e);
      throw (new RuntimeException("Error serializing", e));
    }

deserialization:反序列化:

try (final Input input = new Input(inputStream)) {
      return ((Serializable) kryo.readObject(input, entityType));
    }

entityType is the parent class, in this case: shell.api.model.Heartbeat entityType 是父级 class,在本例中为:shell.api.model.Heartbeat

and, inside Heartbeat are several entities, one of which is BatteryStatus.而且,在 Heartbeat 内部有几个实体,其中之一是 BatteryStatus。

Kryo can handle serializing and deserializing complex nested objects and circular references. Kryo可以处理复杂嵌套对象和循环引用的序列化和反序列化。 It's part of the reason why so many people love Kryo! 这就是为什么这么多人爱Kryo的部分原因!

Since the object you are sending could be one of many possible types you should use the writeClassAndObject and readClassAndObject methods. 由于要发送的对象可能是许多可能的类型之一,因此应使用writeClassAndObjectreadClassAndObject方法。

protected final Kryo kryo = new Kryo();

try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
    try (final Output output = new Output(baos)) {
      kryo.writeClassAndObject(output, data);
      return (baos.toByteArray());
    } catch (IOException e) {
      LOGGER.error("error serializing", e);
      throw (new RuntimeException("Error serializing", e));
    }

And

try (final Input input = new Input(inputStream)) {
  return ((Serializable) kryo.readClassAndObject(input));
}

Docs here 文件在这里

One other possibility is you are using two different versions of Kryo jar in your project and different version classes are being used in serialization and deserialization.另一种可能性是您在项目中使用了两个不同版本的 Kryo jar,并且在序列化和反序列化中使用了不同的版本类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM