title: JAXB - Representing the root element as a list of custom types
body: <p>Similar to this question asked on Stackoverflow, <a href="https://stackoverflow.com/questions/4729385/using-jaxb-to-represent-a-list-as-the-root-element">using JAXB to represent a list as the root element</a>, I have the same issue. Though the solution outlined in the answer doesn't work for me.</p>
  
  <p><em>Note:</em> I am using the Jackson fasterxml lib for doing the mappings.</p>
  
  <p>Essentialy I am consuming some XML from an API (the recurly api). One of the messages returned  by the API has the root element as a list, a demonstration is shown below:</p>
  
  <pre><code>&lt;plans type="array"&gt;
      &lt;plan href="..."&gt;
      ...
      &lt;/plan&gt;
      &lt;plan href="..."&gt;
      ...
      &lt;/plan&gt;
  &lt;/plans&gt;
  </code></pre>
  
  <p>I created the following Java class + JAXB annotations to capture the above:</p>
  
  <pre><code>@XmlRootElement(name = "plans")
  public class Plans extends RecurlyObject {
  
      @XmlElement(name = "plan", type = Plan.class)
      private List&lt;Plan&gt; plans;
  
      public List&lt;Plan&gt; getPlans() { return this.plans; }
  
      public void setPlans(final List&lt;Plan&gt; plans) { this.plans = plans; }
      ...
  }
  </code></pre>
  
  <p>This doesn't work. The docs &amp; articles I have read all demo the above using Strings rather than custom types for the elements of the list.</p>
  
  <p>Running the above generates the following error:</p>
  
  <p>Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value<br>
     of type [simple type, class com.ning.billing.recurly.model.Plan] from JSON String; no 
     single-String constructor/factory method (through reference chain: 
     com.ning.billing.recurly.model.Plans["plan"])</p>
  
  <p>I also tried the following - but generates a similar issue:</p>
  
  <pre><code>@XmlRootElement(name = "plans")
  public class Plans extends RecurlyObject {
      @XmlTransient
      public static final String PLANS_RESOURCE = "/plans";
  
      @XmlElementWrapper(name = "plans")
      @XmlElement(name = "plan", type = Plan.class)
      private List&lt;Plan&gt; plans;
  
      public List&lt;Plan&gt; getPlans() { return this.plans; }
  
      public void setPlans(final List&lt;Plan&gt; plans) { this.plans = plans; }
      ...
  }
  </code></pre>
  
  <p>The only way I can it to not error is if I create a ctor on my Plan class that takes a String. But then my <code>List&lt;Plan&gt;</code> is a list of Plan objects, one for every sub node of all of the Plans :(</p>
  
  <p>BTW - the Plan Java class works fine - I use it to receive individual Plan messages from other API calls.</p>
  
  <p>So, any ideas how to correctly map a root element which is a list of things other than Strings into a Java object using JAXB?</p>
  
  <p>Cheers</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The following solution was provided by <a href="https://github.com/pierre" rel="nofollow noreferrer">Pierre</a> outside of stackoverflow.</p>
  
  <p>It is possible to get this to work with Jackson see the following code:</p>
  
  <pre><code>@XmlRootElement(name = "plans")
  public class Plans extends RecurlyObjects&lt;Plan&gt; {
  }
  </code></pre>
  
  <p>The important details to note is <code>extends RecurlyObjects&lt;Plan&gt;</code>. <code>RecurlyObjects</code> is essentially an <code>ArrayList</code>. </p>
  
  <pre><code>@JsonIgnoreProperties(ignoreUnknown = true)
  public abstract class RecurlyObjects&lt;T extends RecurlyObject&gt; extends ArrayList&lt;T&gt; {
  }
  </code></pre>
  
  <p>This now works fine.</p>