t777 t777 - 4 months ago 29
Java Question

XStream: How to do validation after deserialization

I like to validate my bean using bean-validation after it is read using xstream.

I try to implement readObject(), but this way I have to do the whole deserialization on my own, and that is definitively not what I wanted.

Is there any method called after deserialization, I can override?

Answer

Your approach is all right, you are just missing a few small things.

If you want the default serialization to take place, you can overwrite the readResolve method as you would with standard Java Serialization API

(method private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException) and call in.defaultReadObject(); in that method.

Afterwards you could call your validation methods, or initialize transient fields or do whatever you want after deserialization.

This is also explained in more detail on the XStream FAQ.