How to deal with a Java serialized object whose package changed?

后端 未结 4 935
礼貌的吻别
礼貌的吻别 2020-12-30 02:38

I have a Java class that is stored in an HttpSession object that\'s serialized and transfered between servers in a cluster environment. For the purpose of this explanation,

4条回答
  •  悲&欢浪女
    2020-12-30 03:10

    In some cases you don't have access to the ObjectInputStream, and you can't override readClassDescriptor(). For example, another subsystem serializes and deserializes these objects. In our case, we had legacy instances serialized in Quartz job data maps.

    For this case, you have to maintain a shallow definition of the old class. You can implement the old class's readResolve() and writeReplace() methods.

    class OldClass{
      private int aField;
      private Object readResolve() throws ObjectStreamException {
         return new NewClass(aField);
      }
      private Object writeReplace() throws ObjectStreamException {
         return new NewClass(aField);
      }
    }
    

提交回复
热议问题