title: JAXB: How can derived object be returned as XML element of base type?
body: <p>I write web-services using jax-ws and wsdl contains set of types. Java classes to XML mapps using jaxb. Let's look at the following example:</p>
  
  <pre class="lang-java prettyprint-override"><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "Person")
  public class Person {    
      @XmlElement(name = "firstName")
      protected String firstName;    
      @XmlElement(name = "lastName")
      protected String lastName;
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String value) {
          this.firstName = value;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String value) {
          this.lastName = value;
      }
  }
  </code></pre>
  
  <p>So we have 'Person' XML type that included into WSDL. This class we can name as interface class (for web-service clients). Server code can have a set of derived classes. For example,</p>
  
  <pre class="lang-java prettyprint-override"><code>public class EmployeePerson extends Person {
      protected String post;
  }
  
  public class ClientPerson extends Person {
  }
  </code></pre>
  
  <p>Web-service operation returns object of Person type:</p>
  
  <pre class="lang-java prettyprint-override"><code>@WebMethod(operationName = "GETPERSON")
  public Person getPerson(@WebParam(name = "ID") String personId);
  </code></pre>
  
  <p>Actually we can return one of derived types, but 'Person' XML type should be returned to client (<code>xsi:type='ns:Person'</code>).
  If we use <code>@XmlTransient</code>, object of undefined type will be returned. How can we cast derived class to base type?</p>