Java WS: how to build a ElementNSImpl from a model object?
<p>I have an aplication which is an WS dispatcher done with java JWS that works getting a ws petition, selecting which ws must be called, calling it, getting the response, parsing and returning it back to the client.</p>

<p>To do this, I have a XML composed of a part with info related to my service and another part with specific info of the service that must be called, so 2 diferent calls could be:</p>

<pre><code>&lt;petition&gt;
    &lt;generic&gt;&lt;servicename&gt;ws1&lt;/servicename&gt;&lt;/generic&gt;
    &lt;especific&gt;.... whatever....&lt;/especific&gt;
&lt;/petition&gt;

&lt;petition&gt;
    &lt;generic&gt;&lt;servicename&gt;ws2&lt;/servicename&gt;&lt;/generic&gt;
    &lt;especific&gt;.... whatever needed for ws3....&lt;/especific&gt;
&lt;/petition&gt;
</code></pre>

<p>And their returns;</p>

<pre><code>&lt;return&gt;
  &lt;generic&gt;&lt;servicename&gt;ws1&lt;/servicename&gt;&lt;/generic&gt;
  &lt;specific&gt;&lt;city&gt;bilbao&lt;/city&gt;&lt;/specific&gt;
&lt;/return&gt;


&lt;return&gt;
  &lt;generic&gt;&lt;servicename&gt;ws2&lt;/servicename&gt;&lt;/generic&gt;
  &lt;specific&gt;
   &lt;person&gt;
      &lt;name&gt;Olof&lt;/name&gt;
   &lt;/person&gt;
  &lt;/specific&gt;
&lt;/return&gt;
</code></pre>

<p>To do this I've use the label @XmlAnyElement(lax=false) in the 'specific' field of my model (both in petition and response) so, in my service, I get all the calls as a 'petition' with something inside (the specific part) that will not be parsed by JWS; I will parse it in the specific part of each service called using a marshaller and letting him know which specific model has to be used:</p>

<pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "", propOrder = {})
  @XmlRootElement(name = "petition")
  public class Petition{
      ....
      @XmlAnyElement(lax=false)
      private Object specific;
      ....
  }

 (Same for the response object.)
</code></pre>

<p>This work well when I get the object from the services that I call and return it directly as an ElementNSImpl object in the 'specific' field but usually I have to parse it (usually the specific object that I return is not the same type of the object that is returned to me) and I'm not able to do it.</p>

<p>In that case I have to create a new object (from the type which I want to return), fill it with the info I've obtained and return it to my client. Well, I do it, I build a 'response' setting in it the specific data  object for the service and try to return it but then the WS returns nothing (a 200 HTTP response of length: 0) to the client...</p>

<p>Also, If I set, manually,  the 'specific' part of the response to 'null' the object is well marshalled and my clients receive:</p>

<pre><code>&lt;response&gt;
 &lt;generic&gt;&lt;servicename&gt;ws2&lt;/servicename&gt;&lt;/generic&gt;
&lt;/response&gt;
</code></pre>

<p>So I think that it's waiting for an 'ElementNSImpl' but I don't know how to build it from an object that is JAXB annotated.</p>