I am hoping someone could clarify this for me so that I can better understand how marshalling of JAXB objects work.
From reading and looking at examples it is my understanding that a JAXB object must implement java.io.Serializable
so that the object can be marshalled and unmarshalled correctly. The part that I am a bit unsure about is what happens (read: what are the consequences) when one of your JAXB objects do not implement serializable? This has happened in the past and results in errors in the log files similar to
ERROR [example.package.name.MyJaxbClass.data]DiskStorageFactory.call{503} | Disk Write of -309037891 failed:
java.io.NotSerializableException: example.package.name.myJaxbClass$MyNonSerializedElement
Yet the rest of the code seems to work fine. How important is it for your JAXB classes to implement serializable and what happens when they do not?
If our class does not implement Serializable interface, or if it is having a reference to a non- Serializable class, then the JVM will throw NotSerializableException . All transient and static fields do not get serialized.
What happens if you try to send non-serialized Object over network? When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.
If you want a class object to be serializable, all you need to do it implement the java. io. Serializable interface. Serializable in java is a marker interface and has no fields or methods to implement.
Only implement what you need, and don't try to make your class serializable just for the sake of it. If your object has a reference (transitive or direct) to any non-serializable object, and this reference is not marked with the transient keyword, then your object won't be serializable.
A JAXB (JSR-222) object is not required to implement java.io.Serializable
in order for it to be converted to/from XML. The object to/from XML conversion does not occur as part of Java serialization. Of course if you do wish to serialize the objects for use with some other part of your application then you need to configure them correctly for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With