title: JAXB @XmlElements, different types but same name?
body: <p>I have an Animal class and an extension of Animal called AnimalExtension. </p>
  
  <pre class="lang-java prettyprint-override"><code>public class Animal
  
  public class AnimalExtension extends Animal
  </code></pre>
  
  <p>The only difference between the two classes is that AnimalExtension has another instance variable called animalId. Animal does not have this instance variable. </p>
  
  <p>I also have my own data type that I want to marshal and unamarshal to XML. This data type is called AnimalList. inside AnimalList, there is a list of Animals as an instance variable.</p>
  
  <pre class="lang-java prettyprint-override"><code>@XmlType(name = "AnimalList")
  public class AnimalList{
      private List&lt;Animal&gt; animalList;
      ....
  </code></pre>
  
  <p>animalList can contain both Animal and AnimalExtension. However, on the XML I dont want the element to be named as AnimalExtension; I want them all to have element name of Animal. I only want the extra attribute to show up when JAXB knows that the Animal is actually an instance of AnimalExtension. So if I have a list of that looks like</p>
  
  <pre class="lang-java prettyprint-override"><code>List&lt;Animal&gt; animalList = new LinkedList&lt;Animal&gt;();
  AnimalExtension animalExtension = new AnimalExtension();
  animalExtension.setAnimalId(1);
  amimalExtension.setName("Don");
  
  Animal animal = new Animal();
  animal.setName("Mike");
  animalList.add(animalExtension);
  animalList.add(animal);
  </code></pre>
  
  <p>I want the XML to look like</p>
  
  <pre class="lang-java prettyprint-override"><code>&lt;AnimalList&gt;
     &lt;Animal name="Don" id="1" /&gt;
     &lt;Animal name="Mike" /&gt;
  &lt;/AnimalList&gt;
  </code></pre>
  
  <p>This is what I have tried to do</p>
  
  <pre class="lang-java prettyprint-override"><code>    @XmlElements(
      {
              @XmlElement(name = "Animal", type = Animal.class),
              @XmlElement(name = "Animal", type = AnimalExtension.class)
          }
      )
      public List&lt;Animal&gt; getEntries() {
          return animalList;
      }
  </code></pre>
  
  <p>The code compiles but when I try running my server. It gives me this weird error that is so unrelated to what is going on (BeanCreationException). I tried making the name of the XmlElement to be different for each type and that works, but the challenge here is to make the name the same.</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'encryptionPayloadContentProvider'
  </code></pre>