title: Why does JAXB need a no arg constructor for marshalling?
body: <p>If you try to marshal a class which references a complex type that does not have a no-arg constructor, such as:</p>
  
  <pre><code>import java.sql.Date;
  
  @XmlRootElement(name = "Foo")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Foo {
      int i;
      Date d; //java.sql.Date does not have a no-arg constructor
  }
  </code></pre>
  
  <p>with the JAXB implementation that is part of Java, as follows:</p>
  
  <pre><code>    Foo foo = new Foo();
      JAXBContext jc = JAXBContext.newInstance(Foo.class);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      Marshaller marshaller = jc.createMarshaller();
      marshaller.marshal(foo, baos);
  </code></pre>
  
  <p>JAXB will throw a </p>
  
  <pre><code>com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions java.sql.Date does not have a no-arg default constructor
  </code></pre>
  
  <p>Now, I understand why JAXB needs a no-arg constructor on unmarshalling - because it needs to instantiate the object. But why does JAXB need a no-arg constructor while marshalling?</p>
  
  <p>Also, another nit, why does Java's JAXB implementation throw an exception if the field is null, and isn't going to be marshalled anyway?</p>
  
  <p>Am I missing something or are these just bad implementation choices in Java's JAXB implementation?</p>