偶然间发现有个系统的对外交互的实体对象之前没有显示的声明该类的序列化版本id 。为了后续的代码升级考虑,打算对所有的dto 做一次升级 。 但是,考虑到之前这些dto对象已经被外围的多个系统使用 ,贸然的添加新的VersionId 可能会导致反序列化的失败 。稳妥起见, 打算先计算出系统dto目前在序列化时,系统默认生成的versionId , 然后把获得的id 添加到老的dto中。老的dto代码如下
写了一段获取dto的serialVersionUID 的方法如下(这里我进一步验证了jdk1.5 ,1.6俩个版本间 ,系统默认给出的id 的差异, 结果发现是相同的,具体做法我不详细说了, 就是在eclipse中对jdk版本做切换):
[code]
public class SerialVersionFactory {
public static void main(String args[]){
SerialVersionFactory.getSerialVersionUID(ContractDto.class.getName());
}
private static long getSerialVersionUID(String className){
Class cl = null;
try {
cl = Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("使用的java版本:"+System.getProperty("java.version"));
// Then an ObjectStreamClass for the given class
ObjectStreamClass ocl = ObjectStreamClass.lookup(cl);
long versionUID = ocl.getSerialVersionUID() ;
// And use the ObjectStreamClass to get the Class'
// unique SerialVersionUID
System.out.println("For class " + cl);
System.out.println("static final long serialVersionUID = "
+ versionUID + "L;"); // must be long
// And just for reference...
System.out.println("(Must range from " + Long.MIN_VALUE + " to "
+ Long.MAX_VALUE + ".)");
return versionUID ;
}
}
[/code]
运行结果如下:
[code]
使用的java版本:1.6.0_20
For class class monkey.serial.study.yes.ContractDto
static final long serialVersionUID = -5065686433333130227L;
(Must range from -9223372036854775808 to 9223372036854775807.)
使用的java版本:1.5.0_22
For class class monkey.serial.study.yes.ContractDto
static final long serialVersionUID = -5065686433333130227L;
(Must range from -9223372036854775808 to 9223372036854775807.)
[/code]
可以看到 ,jdk不同版本间 ,生成出来的versionid是一致的 。另外, 我在contractDto中新增加了一个属性,如下
[code]
public class ContractDto implements Serializable{
private String contractNo ;
private String companyName ;
public static int amount = 1000 ;
public String test = "abc" ;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
ContractDto.amount = amount;
}
public String getContractNo() {
return contractNo;
}
public void setContractNo(String contractNo) {
this.contractNo = contractNo;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
[/code]
在此运行测试代码, 结果如下:
[code]
使用的java版本:1.6.0_20
For class class monkey.serial.study.yes.ContractDto
static final long serialVersionUID = -8371732973844926670L;
(Must range from -9223372036854775808 to 9223372036854775807.)
[/code]
这个时候生成的versiionid 随着实体的变化也发生了变化 。
public class ContractDto implements Serializable{
private String contractNo ;
private String companyName ;
public static int amount = 1000 ;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
ContractDto.amount = amount;
}
public String getContractNo() {
return contractNo;
}
public void setContractNo(String contractNo) {
this.contractNo = contractNo;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
写了一段获取dto的serialVersionUID 的方法如下(这里我进一步验证了jdk1.5 ,1.6俩个版本间 ,系统默认给出的id 的差异, 结果发现是相同的,具体做法我不详细说了, 就是在eclipse中对jdk版本做切换):
[code]
public class SerialVersionFactory {
public static void main(String args[]){
SerialVersionFactory.getSerialVersionUID(ContractDto.class.getName());
}
private static long getSerialVersionUID(String className){
Class cl = null;
try {
cl = Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("使用的java版本:"+System.getProperty("java.version"));
// Then an ObjectStreamClass for the given class
ObjectStreamClass ocl = ObjectStreamClass.lookup(cl);
long versionUID = ocl.getSerialVersionUID() ;
// And use the ObjectStreamClass to get the Class'
// unique SerialVersionUID
System.out.println("For class " + cl);
System.out.println("static final long serialVersionUID = "
+ versionUID + "L;"); // must be long
// And just for reference...
System.out.println("(Must range from " + Long.MIN_VALUE + " to "
+ Long.MAX_VALUE + ".)");
return versionUID ;
}
}
[/code]
运行结果如下:
[code]
使用的java版本:1.6.0_20
For class class monkey.serial.study.yes.ContractDto
static final long serialVersionUID = -5065686433333130227L;
(Must range from -9223372036854775808 to 9223372036854775807.)
使用的java版本:1.5.0_22
For class class monkey.serial.study.yes.ContractDto
static final long serialVersionUID = -5065686433333130227L;
(Must range from -9223372036854775808 to 9223372036854775807.)
[/code]
可以看到 ,jdk不同版本间 ,生成出来的versionid是一致的 。另外, 我在contractDto中新增加了一个属性,如下
[code]
public class ContractDto implements Serializable{
private String contractNo ;
private String companyName ;
public static int amount = 1000 ;
public String test = "abc" ;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
ContractDto.amount = amount;
}
public String getContractNo() {
return contractNo;
}
public void setContractNo(String contractNo) {
this.contractNo = contractNo;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
[/code]
在此运行测试代码, 结果如下:
[code]
使用的java版本:1.6.0_20
For class class monkey.serial.study.yes.ContractDto
static final long serialVersionUID = -8371732973844926670L;
(Must range from -9223372036854775808 to 9223372036854775807.)
[/code]
这个时候生成的versiionid 随着实体的变化也发生了变化 。