excluding byte[] from serialization of XMLEncoder

吃可爱长大的小学妹 提交于 2019-12-12 01:42:00

问题


The problem is how to except an byte[] from serialization of XMLEncoder, but i need to save this field to DB. I have a Object

public class MyClass1 implements Serializable {
 some properties ...
 private  byte[] a01_14_01_content;
 getters and setters ...
}

and Encoder:

import java.beans.XMLEncoder;
public class MyEncoder{ 
...
public byte[] getBytes() {
    XMLEncoder e = new XMLEncoder(baos);
    e.writeObject(answer);
    e.close();
    return baos.toByteArray();
}
}

I need to serialize all fields except array fields. transient modifier for property doesn't work; @Transient annotation on on get method doesn't work; @XMLTransient annotation on property doesn't work. It's so simple, but I need help of community!


回答1:


Answer is to use @java.beans.Transient annotaion on get method instead @Transient. In my case import javax.persistence.* caused a "bug" ))

public class MyClass1 implements Serializable {
 some properties ...
 private  byte[] a01_14_01_content;

 @javax.beans.Transient //not @Transient
 public byte[] getA01_14_01_content() {
 return a01_14_01_content;
 } 
//getters and setters ...
}


来源:https://stackoverflow.com/questions/35173074/excluding-byte-from-serialization-of-xmlencoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!