JAXB xjc:simple binding does not generate @XmlRootElement for base class
16:32 16 Jun 2015

I am using JAXB 2.0. I have various elements and types defined in an XSD file. Here's an example:



    




    
        
             
        
    




    
        
 
     
            
     

My intended use of these objects is:

  1. A Person may appear as a solitary Marshalled element, or may be a part of a People object
  2. If a Person is part of a People object, then the xsi:type should indicate whether it's just a normal Person or a Musician.

So I need the generated Java classes to contain both an @XmlRootElement annotation as well as an @XmlType annotation. The xjc:simple binding creates both annotations for a Musician, but only creates the @XmlType for Person. So when I Marshall a Person object, all I get is:


Whereas what I would like to see is:



For a People object, I want to see:



    
    

I have read about the simple binding with xjc, and it works with all the lowest levels in an inheritance hierarchy. However, the base classes end up without an @XmlRootElement annotation. For the use case I'm working on, it's imperative that base classes can be Marshalled as both a top-level element and as a member of other elements. Any suggestions would be welcome.

java xml jaxb