JaxB is very troublesome when it comes to template classes and abstract classes. The main problems are serializations containing xsi and xmlns annotations. Such annotations make the xml unreadable and rigid (resisting future changes).

List (java.util) combined with abstract classes can be made to work, with a number of annotations. Assume class X is an abstract class with X1 and X2 as implementations.

Then

  • annotate all occurences List<X> with @XmlElementRef
  • Annotate all "X var" fields with @XmlElementRef
  • add @XmlSeeAlso({X1.class, X2.class}) to the X class
  • add @XmlType to the X class (after the @XmlSeeAlso)
  • You can put partial implementations and @XmlElement's in the X class as well
  • annotate X1 and X2 with @XmlRootElement (and maybe give it a nicer name there too)
  • Put class specific @XmlElement's in X1 and X2

General templates like Myclass<T extends Object> do not serialize nicely. This is because erasure causes the class to serialize 'Object' and deserializing 'Object' will probably result in xsi stuff. What you can do is to do

  • Myclass<T extends X>
  • handle all serialization in class X (not in the T classes)

This means that you can't have class-specific deserializations. Maybe you can combine this with the tricks used with List (see above), I never figured this out

Last modified 7 years ago Last modified on 05/31/17 16:58:30
Note: See TracWiki for help on using the wiki.