Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the consequences of a JAXB object not implementing serializable?

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?

like image 987
ae14 Avatar asked Mar 21 '13 13:03

ae14


People also ask

What will happen if we don't implement serializable?

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 object is not 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.

Is it necessary to implement serializable?

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.

What do you do if an object should not or does not need to be serialized?

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.


1 Answers

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.

like image 122
bdoughan Avatar answered Sep 21 '22 18:09

bdoughan