最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Java代码警告:The serializable class does not declare a static final serialVersionUID field of type long

class crifan 639浏览 0评论
折腾:
【未解决】VSCode中调试java报错:Build failed do you want to continue
期间,看到问题中显示:
The serializable class CustomException does not declare a static final serialVersionUID field of type long
点击看看:
The serializable class does not declare a static final serialVersionUID field of type long
异常警告The serializable class * does not declare a static final serialVersionUID – 为了美好的明天的个人空间 – OSCHINA
可以解决,也可以去设置忽略此警告
为了代码更好,还是实现
VSCode中,鼠标移动到类上,点击 快速修复
选择 Add generated serial version ID
即可自动插入代码:
src/refer/java/iec_analysis/src/main/java/com/iec/analysis/exception/CustomException.java
private static final long serialVersionUID = 1008826607851521180L;
即可。解决。
还有其他很多个文件报此错误:
按照此法,都去加上:
继续去修复其他文件即可。
java – What does it mean: The serializable class does not declare a static final serialVersionUID field? – Stack Overflow
“The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender’s class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named “serialVersionUID” that must be static, final, and of type long:”
Serializable (Java Platform SE 6)
java – how to remove the warning The serializable class ClASSNAME does not declare a static final serialVersionUID field of type long – Stack Overflow
如果要忽略掉单个class,可以加上:
@SuppressWarnings("serial")
Eclise中忽略的话,是:
Eclipse ->Preferences->Java->Compiler->Errors/Warnings->Potential programming problems->Serializable class without serialVersionUID -> change warning to ignore
或设置一个固定的值,比如
public static final long serialVersionUID = 1L;
java – What does the “declare a static final serialVersionUID” warning mean and how to fix? – Stack Overflow
Eclipse中自动生成则是:Source Cleanup…->Custome Profile->Missing Code->Add serial version ID
java – The serializable class Employee does not declare a static final serialVersionUID field of type long – Stack Overflow
eclipse – Java warning: “The serializable class Info does not declare a static final serialVersionUID field of type long” and 2 errors – Stack Overflow
关于The serializable class XXX does not declare a static final serialVersionUID field of type long的警告 – Johnny Qian – 博客园
【总结】
java代码中,当一个类实现了Serializable接口,如果没有实现serialVersionUID,则会报警告
The serializable class xxx does not declare a static final serialVersionUID field of type long
原因:
主要是用来序列化和反序列化时,确保版本号一致,才能确保序列化和反序列化是一致的,数据正确的。
解决办法:
  • 忽略此警告
    • 对于单个类,加上:@SuppressWarnings(“serial”)
    • 对于IDE来说,可以设置全局的忽略
      • Eclipse
        • Preferences->Java->Compiler->Errors/Warnings->Potential programming problems->Serializable class without serialVersionUID -> change warning to ignore
  • 设置对应的值
    • 设置一个默认的值
      • 比如设置成1:
        • 代码:private static final long serialVersionUID = 1L;
        • 方式:
          • 自己手动加上:
          • 利用IDE自动加上
            • VSCode:鼠标移动到类名上-》快速修复-》Add default serial version ID
    • 设置一个(根据类名等信息)自动生成的值
      • 用IDE自动生成
        • VSCode:鼠标移动到类名上-》快速修复-》Add generated serial version ID
        • Eclipse:Add generated serial version ID
自己推荐用:Add generated serial version ID
好处:确保不会重复,且利于以后万一真用到了serial序列号。更准确。

转载请注明:在路上 » 【已解决】Java代码警告:The serializable class does not declare a static final serialVersionUID field of type long

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.120 seconds, using 20.50MB memory