简体   繁体   English

java.lang.Integer 的 Java 反序列化 - 异常

[英]Java Deserialization of java.lang.Integer - Exception

Recieved the following exception when deserializing a HashMap<String, Integer> :反序列化HashMap<String, Integer>时收到以下异常:

java.io.InvalidClassException: java.lang.Integer; local class incompatible: stream classdesc serialVersionUID = 1360826667802527544, local class serialVersionUID = 1360826667806852920

Serialized and deserialized on the same machine, with the same JRE.在同一台机器上使用相同的 JRE 进行序列化和反序列化。 JDK 1.6.0_12 JDK 1.6.0_12

From looking at the JDK source, 1360826667806852920 is the correct serialVersionUID for Integer .从 JDK 源代码来看, 1360826667806852920 是Integer的正确serialVersionUID I wasn't able to find any classes in the JDK with the serialVersionUID 1360826667802527544.我无法在 JDK 中使用serialVersionUID 1360826667802527544 找到任何类。

Interestingly, searching for 1360826667802527544 on Google turned up a few other people with this problem, notably this thread on Sun's forums.有趣的是,在 Google 上搜索 1360826667802527544 时发现了一些其他人遇到了这个问题,尤其是 Sun 论坛上的这个帖子。 The problem there was that the person was storing bytes in a String, and the serialized data was getting mangled.那里的问题是这个人将字节存储在一个字符串中,并且序列化的数据被破坏了。 Since you're getting the same serialVersionUID it seems very likely that you're running into a similar problem.由于您获得了相同的serialVersionUID ,您似乎很可能遇到了类似的问题。

Never store bytes in a String .永远不要将字节存储在String中。 Use a byte array or a class designed to hold bytes, not chars.使用字节数组或设计用于保存字节而不是字符的类。

I faced the same issue and it is because when we are trying to store Integer object to String, the character encoding is getting messed up and while deserialization the serialVersionUID read is wrong.我遇到了同样的问题,这是因为当我们尝试将 Integer 对象存储到 String 时,字符编码变得混乱,并且在反序列化时 serialVersionUID 读取是错误的。 That's the root cause of this error.这就是这个错误的根本原因。 To avoid this error use Base64 encoding before storing it to String.为避免此错误,请在将其存储到 String 之前使用Base64编码 see this answer and the problem resolved for me.看到这个答案,问题就为我解决了。

        /** Read the object from Base64 string. */
   private static Object fromString( String str ) throws IOException, ClassNotFoundException {
        byte [] data = Base64.getDecoder().decode(str);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
        Object o = ois.readObject();
        ois.close();
        return o;
   }

    /** Write the object to a Base64 string. */
    private static String toString( Serializable o ) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream( baos );
        oos.writeObject( o );
        oos.close();
        return Base64.getEncoder().encodeToString(baos.toByteArray()); 
    }

check the source code for Integer, here is what I have for Integer in several verions of java:检查 Integer 的源代码,这是我在几个 java 版本中对 Integer 的代码:

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1360826667806852920L;

So I'd say the problem comes from a class of yours that you changed between serialization and deserialization and that has no specific serialVersionUID...所以我会说问题来自你的一类,你在序列化和反序列化之间进行了更改,并且没有特定的serialVersionUID ...

Maybe you should look at this , same problem description and it looks like wrong serialization / deserialization code....也许你应该看看这个,同样的问题描述,它看起来像错误的序列化/反序列化代码......

That shouldn't happen.那不应该发生。 Note that the IDs differ only in the last few digits;请注意,ID 仅在最后几位数字上有所不同; the second one ist the one I see in my JDK sources.第二个是我在我的 JDK 源代码中看到的那个。

My guess is that the serialized stream got corrupted somehow.我的猜测是序列化流以某种方式损坏了。

I've run into the same issue on a compiled jasperreport.我在编译的 jasperreport 上遇到了同样的问题。 The packed ear on the server was corrupted due to the filtering of the ant build.由于 ant build 的过滤,服务器上的 pack ear 已损坏。 As a result, the original jasperreport file with the one on the ear had some differences.结果,原始的jasperreport文件与耳朵上的那个有一些差异。

I've modified the ant build to copy only the file (instead of filtering) and not filter the我已修改 ant 构建以仅复制文件(而不是过滤)而不过滤

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

相关问题 无法在Hibernate中将java.lang.Integer字段…User.id设置为java.lang.Integer异常 - Can not set java.lang.Integer field …User.id to java.lang.Integer exception in Hibernate java.lang.Integer无法转换为java.lang.String异常 - java.lang.Integer cannot be cast to java.lang.String exception 异常:java.lang.String无法转换为java.lang.Integer - Exception: java.lang.String cannot be cast to java.lang.Integer java.lang.Integer内部代码中的一个问题 - A question in java.lang.Integer internal code java.lang.Integer无法转换为JSONObject - java.lang.Integer cannot be converted to JSONObject 线程“ main”中的异常java.lang.ClassCastException:java.lang.Integer无法转换为q3.Box - Exception in thread “main” java.lang.ClassCastException: java.lang.Integer cannot be cast to q3.Box 线程“main”中的异常 java.lang.NoSuchMethodError: &#39;void Car.setYear(java.lang.Integer)&#39; - Exception in thread "main" java.lang.NoSuchMethodError: 'void Car.setYear(java.lang.Integer)' 模型映射器异常:仅在 Docker 中无法将 java.lang.String 转换为 java.lang.Integer - Model Mapper Exception: failed to convert java.lang.String to java.lang.Integer only in Docker 什么是java.lang.Integer保留的大小? - What is the java.lang.Integer retained size? ClassCastException:无法转换为java.lang.Integer - ClassCastException : cant be cast to java.lang.Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM