title: JAXB element that is both optional and nillable
body: <p>I have re-formatted the question to hopefully make my intentions clearer.</p>
  
  <p><strong>Architecture</strong><br>
  I'm writing some web services that I will be publishing myself using JAX-WS. The process we have been using for some time is to first write a schema that only defines the request and response objects. This gets sent to the customer to approve the structure of the xml messages. I don't want to write the whole wsdl myself as it's more complicated than the basic schema.</p>
  
  <p>Next I use the JAXB command xjc to generate classes based on the request and response types in my schema. I then use this classes as parameters and return types on a JAX-WS annotated endpoint class.</p>
  
  <p>This now gives me a web service I can call. It gives me more control over the xml being sent and returned but also automates the repetition required in writing the full wsdl.</p>
  
  <p><strong>Problem</strong><br>
  In the schema I have an element like this:</p>
  
  <pre><code>&lt;xs:element name="myElement" type="xs:string" nillable="true" minOccurs="0" /&gt; 
  </code></pre>
  
  <p>So I want to distinguish between the user setting null or blank. The generated class then has this attribute.</p>
  
  <pre><code>@XmlElementRef(name = "myElement", namespace = "/mynamespace", type = JAXBElement.class)
  protected JAXBElement&lt;String&gt; myElement;
  </code></pre>
  
  <p>The effect of this is that the element becomes neither nillable or optional. The schema that JAX-WS writes as part of the wsdl has set the element to mandatory and not nillable and if I turn off schema validation I still can't pass nil through to my object. </p>
  
  <p><strong>Things tried</strong><br>
  If I change it to be required and nillable then I get this generated code.</p>
  
  <pre><code>@XmlElement(required = true, nillable = true)
  protected String myElement;
  </code></pre>
  
  <p>If I change it to optional and not nillable then I get this generated code.</p>
  
  <pre><code>protected String myElement
  </code></pre>
  
  <p>So you can have either or but not both it seems if you use JAXB. Thoroughly disappointing!</p>
  
  <p>I've also tried manually changing the generated class to look like this.</p>
  
  <pre><code>@XmlElementRef(name = "myElement", namespace = "/mynamespace", type = JAXBElement.class, required=false)
  protected JAXBElement&lt;String&gt; myElement;
  </code></pre>
  
  <p>This now makes the element optional but I still can't set it to nil. Doing so results in a JAXBElement with a value of a blank string. That's only if you turn schema validation off as the resulting JAX-WS wsdl/schema doesn't set the element as nillable, so its not a valid request.</p>
  
  <p><strong>Summary</strong><br>
  It's my belief that this is a bug with JAXB. The @XmlElementRef annotation has an attribute to set it as not required but there is no attribute to set the field as nullable. </p>
  
  <p>The @XmlElement annotation has attributes for both required and nullable but these just result in a null object so there would be no way to distinguish between an element not included in the xml or an element that was included but null. This is why you need to use @XmlElementRef along with JAXBElement.</p>
  
  <p>I think the bug includes two issues. First the xjc command should generate the element with required=false. Second there should be an attribute on @XmlElementRef to set whether the element is nullable and this should be set too.</p>
  
  <p>Does anyone know of a fix/workaround? I tried googling but only found people asking the same question without an answer. This usually means it's not possible... TIA.</p>
  
  <p><strong>Additional</strong><br>
  I'm using jaxb 2.2.6 and the maven plugin is jaxb2-maven-plugin 1.5.</p>