I’m looking for a way to serialize Java objects into XML for use by a RESTful web service. I don’t have an XSD.
I have looked at the following:
-
JAXB – fairly heavy weight with annotations required on classes and also an ObjectFactory class and/or a jaxb.index file
-
Simple – requires annotations but no other config classes/files. Unfortunately it can’t serialize Sets.
-
XStream – no annotations etc. required, but doesn’t support generics
Does anyone else have any suggestions?
My vote would be for XStream. The lack of generics support is a small price to pay for the amount of flexibility it offers. You can also easily implement generics by serializing the generic class type at serialization time, or build this into your domain objects. E.g.
In the stream, the element type denotes the type of each object. When the type is an abstract type or interface, the element listed with the implementing class, unless that has been specified as the default for that interface type. (E.g. ArrayList as the default for instances of static type List.)
Generics are “rose coloured glasses” in java – they don’t really change what you are seeing, just how you see it. The objects would be sent over the wire exactly the same if they were sent with generics support or not.