title: How to ignore/disable/revert/override JAXB class-level @XmlJavaTypeAdapter in certain situations?
body: <p>Our model classes are annotated with @XmlJavaTypeAdapter (at the class-level). Unmarshalling works fine for the root element and containment/nesting (according to what we implemented in our custom XmlAdapter).</p>
  
  <p>So far, we were happy campers for both XML and JSON serialization/deserialization. However, a new need arose and I can't figure out how to implement it ?</p>
  
  <p>In certain situations, I'd like to be able to "revert" to default JAXB behavior for containment: I want the class-level @XmlJavaTypeAdapter annotation to be ignored/overriden.</p>
  
  <p>I spent hours reading Blaise Doughan's blog (<a href="http://blog.bdoughan.com/" rel="nofollow">http://blog.bdoughan.com/</a>) and searching StackOverflow and Google but can't find an elegant/pragmatic solution.</p>
  
  <p>Here is a quick setup to illustrate what we currently have (please note that all our JPA/Hibernate/other annotations are not listed for simplicity-sake but they do exist in our model classes (POJOs)):</p>
  
  <p>Class Master</p>
  
  <pre><code>@XmlRootElement
  @XmlAccessorType(XmlAccessType.NONE)
  @XmlJavaTypeAdapter(XmlMasterAdapter.class)
  public class Master {
      @XmlElement
      private Long masterPrimaryKey;
  
      @XmlElement
      private String name;
  }
  </code></pre>
  
  <p>Class Detail</p>
  
  <pre><code>@XmlRootElement
  @XmlAccessorType(XmlAccessType.NONE)
  @XmlJavaTypeAdapter(XmlDetailAdapter.class)
  public class Detail {
      @XmlElement
      private Long detailPrimaryKey;
  
      @XmlElement
      private Master master; // reference/foreign key. No need for @XmlJavaTypeAdapter since it's defined at the class-level in Master.
  
      @XmlElement
      private String value;
  }
  </code></pre>
  
  <p>When Master is used as a the root element, the XML is like this:</p>
  
  <pre><code>&lt;master&gt;
      &lt;masterPrimaryKey&gt;1234&lt;/masterPrimaryKey&gt;
      &lt;name&gt;master name&lt;/name&gt;
  &lt;/master&gt;
  </code></pre>
  
  <p>When Master is used as a contained/nested element, the XML is like this: (thanks to our custom XmlAdapter, the &lt;master&gt; element is "summarized" by its primary key)</p>
  
  <pre><code>&lt;detail&gt;
      &lt;detailPrimaryKey&gt;5678&lt;/detailPrimaryKey&gt;
      &lt;master&gt;1234&lt;/master&gt;
      &lt;value&gt;detail value&lt;/value&gt;
  &lt;/detail&gt;
  </code></pre>
  
  <p>So far, everything works fine and we're happy with it.</p>
  
  <p>Now, our new need:</p>
  
  <p>I'd like containment to work in a different way in specific situations.
  I want the class-level @XmlJavaTypeAdapter on Master to "temporarily" be ignored/reverted/overridden in a specific context. I'd expect the default JAXB unmarshaller to kick-in (as if there had never been a class-level @XmlJavaTypeAdapter on the contained classes).</p>
  
  <p>Think about a data-import situation where we receive the master and all the details in one payload. As if they were all independent root elements wrapped in a big DTO/transport container.</p>
  
  <p>Here is the XML presenting what we want:</p>
  
  <pre><code>&lt;masterDetailImport&gt;
      &lt;master&gt;
          &lt;!-- Primary keys omitted because of the import mode --&gt;
          &lt;name&gt;master name&lt;/name&gt;
      &lt;/master&gt;
  
      &lt;details&gt;
          &lt;detail&gt;
              &lt;value&gt;detail 1 value&lt;/value&gt;
          &lt;/detail&gt;
  
          &lt;detail&gt;
              &lt;value&gt;detail 2 value&lt;/value&gt;
          &lt;/detail&gt;
  
          &lt;detail&gt;
              &lt;value&gt;detail 3 value&lt;/value&gt;
          &lt;/detail&gt;
      &lt;/details&gt;
  &lt;/masterDetailImport&gt;
  </code></pre>
  
  <p>Class MasterDetailImport</p>
  
  <pre><code>@XmlRootElement
  @XmlAccessorType(XmlAccessType.NONE)
  public class MasterDetailImport implements Serializable 
  {
      @XmlElement
      @PLEASE_IGNORE_CLASS_LEVEL_XmlJavaTypeAdapter_AND_UNMARSHAL_AS_IF_IT_WERE_A_ROOT_ELEMENT
      private Master master;
  
      @XmlElementWrapper(name="details")
      @XmlElement
      @PLEASE_IGNORE_CLASS_LEVEL_XmlJavaTypeAdapter_AND_UNMARSHAL_AS_IF_IT_WERE_A_ROOT_ELEMENT
      private List&lt;Detail&gt; detail = new ArrayList&lt;Detail&gt;();
  }
  </code></pre>
  
  <p>What I'm looking for is the magic [yet non-existing] @PLEASE_IGNORE_CLASS_LEVEL_XmlJavaTypeAdapter_AND_UNMARSHAL_AS_IF_IT_WERE_A_ROOT_ELEMENT annotation that would allow me to instruct JAXB to do as if @XmlJavaTypeAdapter had never been defined at the class-level for the nested classes.</p>
  
  <p>So far, the solutions we envisioned [and don't like] are:</p>
  
  <ol>
  <li><p>Create "mirror" DTO objects for deserialization only when we must support import. The are many cons with this approach (duplicate code only used for deserialization, adapters to copy the DTO content into the model class, more unit tests to write/maintain, etc).</p></li>
  <li><p>Get rid of class-level @XmlJavaTypeAdapter on all our entities we want to be able to import/nest and explicitly use @XmlJavaTypeAdapter on all attributes where nesting/containment is used. I tested this approach and know it would work. However, I think it's error prone and not as elegant as defining it at class-level and be able to have an exception/special-case/override handling telling JAXB to temporarily behave as if it never knew @XmlJavaTypeAdapter has been defined on the class.</p></li>
  </ol>
  
  <p>I'm running out of ideas here... I tried looking for JAXB's default XML adapter but was not successful: javax.xml.bind.annotation.adapters.XmlAdapter&lt;ValueType,BoundType&gt; is abstract and inherits from Java.lang.Object.</p>
  
  <p>Now, the simple question:
  How to implement @PLEASE_IGNORE_CLASS_LEVEL_XmlJavaTypeAdapter_AND_UNMARSHAL_AS_IF_IT_WERE_A_ROOT_ELEMENT ?</p>
  
  <p>Thanks in advance !</p>