title: JAXB, how to validate nillable and required field when unmarshalling
body: <p>I have a small problem with JAXB, but unfortunately I was not able to find answer.</p>
  
  <p>I have a class Customer, with 2 fields <strong>name</strong> and <strong>city</strong>, the mapping is done using annotations and both fields are marked as required and not nillable. </p>
  
  <pre><code>@XmlRootElement(name = "customer")
  public class Customer {
  
      enum City {
          PARIS, LONDON, WARSAW
      }
  
      @XmlElement(name = "name", required = true, nillable = false)
      public String name;
      @XmlElement(name = "city", required = true, nillable = false)
      public City city;
  
      @Override
      public String toString(){
          return String.format("Name %s, city %s", name, city);
      }
  }
  </code></pre>
  
  <p>However, when I submit such XML file:</p>
  
  <pre><code>&lt;customer&gt;
      &lt;city&gt;UNKNOWN&lt;/city&gt;
  &lt;/customer&gt;
  </code></pre>
  
  <p>I will receive a Customer instance with both fields set to null. </p>
  
  <p>Shouldn't there be a validation exception or am I missing something in the mapping?</p>
  
  <p>To unmarshal I use:</p>
  
  <pre><code>JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  Customer customer = (Customer) unmarshaller.unmarshal(in);
  </code></pre>