excluding byte[] from serialization of XMLEncoder

2019-09-10 00:50发布

问题:

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 ...
}