JAX-WS works in a very simple and effective way if you have defined all objects in a XML Schema definition. But sometimes you can’t define a schema for an operation because e.g. it is a generic operation and accepts or returns dynamic XML.
Nevertheless we would like to use for this operation the same tool chain with JAX-WS which is working perfectly for other operations.
In the first step we define the interface of the operation testXMLCall in the WSDL (better the XSD referenced by the WSDL).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;"><</span><span style="color:#800000;">xsd:element </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">='testXMLCall'> </span><span style="color:#008080;"> 2</span> <span style="color:#0000FF;"> </span><span style="color:#FF0000;"><xsd:complexType</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;"> 3</span> <span style="color:#000000;"> </span><span style="color:#0000FF;"><</span><span style="color:#800000;">xsd:sequence</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;"> 4</span> <span style="color:#000000;"> </span><span style="color:#0000FF;"><</span><span style="color:#800000;">xsd:element </span><span style="color:#FF0000;">minOccurs</span><span style="color:#0000FF;">='1' </span><span style="color:#FF0000;">maxOccurs</span><span style="color:#0000FF;">='1' </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">='name' </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">='xsd:string'/> </span><span style="color:#008080;"> 5</span> <span style="color:#0000FF;"> </span><span style="color:#FF0000;"><xsd:element minOccurs</span><span style="color:#0000FF;">='1' </span><span style="color:#FF0000;">maxOccurs</span><span style="color:#0000FF;">='1' </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">='requestXMLData' </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">='xsd:anyType'/> </span><span style="color:#008080;"> 6</span> <span style="color:#0000FF;"> </span><span style="color:#FF0000;"></xsd:sequence</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;"> 7</span> <span style="color:#000000;"> </span><span style="color:#0000FF;"></</span><span style="color:#800000;">xsd:complexType</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;"> 8</span> <span style="color:#000000;"></span><span style="color:#0000FF;"></</span><span style="color:#800000;">xsd:element</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;"> 9</span> <span style="color:#000000;"></span><span style="color:#0000FF;"><</span><span style="color:#800000;">xsd:element </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">='testXMLCallResponse'> </span><span style="color:#008080;">10</span> <span style="color:#0000FF;"> </span><span style="color:#FF0000;"><xsd:complexType</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;">11</span> <span style="color:#000000;"> </span><span style="color:#0000FF;"><</span><span style="color:#800000;">xsd:sequence</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;">12</span> <span style="color:#000000;"> </span><span style="color:#0000FF;"><</span><span style="color:#800000;">xsd:element </span><span style="color:#FF0000;">minOccurs</span><span style="color:#0000FF;">='1' </span><span style="color:#FF0000;">maxOccurs</span><span style="color:#0000FF;">='1' </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">='responseXMLData' </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">='xsd:anyType'/> </span><span style="color:#008080;">13</span> <span style="color:#0000FF;"> </span><span style="color:#FF0000;"></xsd:sequence</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;">14</span> <span style="color:#000000;"> </span><span style="color:#0000FF;"></</span><span style="color:#800000;">xsd:complexType</span><span style="color:#0000FF;">></span><span style="color:#000000;"> </span><span style="color:#008080;">15</span> <span style="color:#000000;"></span><span style="color:#0000FF;"></</span><span style="color:#800000;">xsd:element</span><span style="color:#0000FF;">></span></div> |
From this WSDL we generate the interface PortType of the webservice. The implementation of the interface needs an operation of this definition:
1 2 3 4 5 6 7 8 9 |
<div><span style="color:#008080;">1</span> <span style="color:#000000;">@WebMethod(action </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://localhost/testXMLCall</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;">2</span> <span style="color:#000000;">@WebResult(name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">responseXMLData</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;">3</span> <span style="color:#000000;">@RequestWrapper(localName </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">testXMLCall</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">, className </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">com.oc.soa.sample.ws.v1.messages.TestXMLCall</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;">4</span> <span style="color:#000000;">@ResponseWrapper(localName </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">testXMLCallResponse</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">, className </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">com.oc.soa.sample.ws.v1.messages.TestXMLCallResponse</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;">5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">public</span><span style="color:#000000;"> Object testXMLCall( </span><span style="color:#008080;">6</span> <span style="color:#000000;"> @WebParam(name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">name</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;">7</span> <span style="color:#000000;"> String name, </span><span style="color:#008080;">8</span> <span style="color:#000000;"> @WebParam(name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">requestXMLData</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;">9</span> <span style="color:#000000;"> Object requestXMLData);</span></div> |
We make an implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<div><span style="color:#008080;"> 1</span> <span style="color:#000000;">@WebMethod(action </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://localhost/testXMLCall</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;"> 2</span> <span style="color:#000000;">@WebResult(name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">responseXMLData</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;"> 3</span> <span style="color:#000000;">@RequestWrapper(localName </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">testXMLCall</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">, className </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">com.oc.soa.sample.ws.v1.messages.TestXMLCall</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;"> 4</span> <span style="color:#000000;">@ResponseWrapper(localName </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">testXMLCallResponse</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">, className </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">com.oc.soa.sample.ws.v1.messages.TestXMLCallResponse</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">public</span><span style="color:#000000;"> Object testXMLCall( </span><span style="color:#008080;"> 6</span> <span style="color:#000000;"> @WebParam(name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">name</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;"> 7</span> <span style="color:#000000;"> String name, </span><span style="color:#008080;"> 8</span> <span style="color:#000000;"> @WebParam(name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">requestXMLData</span><span style="color:#000000;">"</span><span style="color:#000000;">, targetNamespace </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">http://opitz-consulting.com/interfaces/TestMessages/V1</span><span style="color:#000000;">"</span><span style="color:#000000;">) </span><span style="color:#008080;"> 9</span> <span style="color:#000000;"> Object requestXMLData) { </span><span style="color:#008080;">10</span> <span style="color:#000000;"> TestXMLCallResponse result </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> TestXMLCallResponse(); </span><span style="color:#008080;">11</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (requestXMLData </span><span style="color:#0000FF;">instanceof</span><span style="color:#000000;"> Element) { </span><span style="color:#008080;">12</span> <span style="color:#000000;"> Element requestXMLDataElement </span><span style="color:#000000;">=</span><span style="color:#000000;"> ((Element) requestXMLData); </span><span style="color:#008080;">13</span> <span style="color:#000000;"> LOG.info(</span><span style="color:#000000;">"</span><span style="color:#000000;">XML in: n</span><span style="color:#000000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> getPrintString(requestXMLDataElement)); </span><span style="color:#008080;">14</span> <span style="color:#000000;"> XmlObject xmlResult; </span><span style="color:#008080;">15</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">try</span><span style="color:#000000;"> { </span><span style="color:#008080;">16</span> <span style="color:#000000;"> xmlResult </span><span style="color:#000000;">=</span><span style="color:#000000;"> doSomething(requestXMLDataElement); </span><span style="color:#008080;">17</span> <span style="color:#000000;"> } </span><span style="color:#0000FF;">catch</span><span style="color:#000000;"> (RuntimeException e) { </span><span style="color:#008080;">18</span> <span style="color:#000000;"> LOG.error(</span><span style="color:#000000;">"</span><span style="color:#000000;">Error while doSomething with XML: n</span><span style="color:#000000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> getPrintString(requestXMLDataElement), e); </span><span style="color:#008080;">19</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">throw</span><span style="color:#000000;"> e; </span><span style="color:#008080;">20</span> <span style="color:#000000;"> } </span><span style="color:#008080;">21</span> <span style="color:#000000;"> LOG.info(</span><span style="color:#000000;">"</span><span style="color:#000000;">XML out: n</span><span style="color:#000000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> getPrintString(xmlResult.getDomNode())); </span><span style="color:#008080;">22</span> <span style="color:#000000;"> result.setResponseXMLData(xmlResult.getDomNode().getFirstChild()); </span><span style="color:#008080;">23</span> <span style="color:#000000;"> } </span><span style="color:#0000FF;">else</span><span style="color:#000000;"> </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (requestXMLData </span><span style="color:#000000;">==</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">) { </span><span style="color:#008080;">24</span> <span style="color:#000000;"> LOG.error(</span><span style="color:#000000;">"</span><span style="color:#000000;">Data is null.</span><span style="color:#000000;">"</span><span style="color:#000000;"> ); </span><span style="color:#008080;">25</span> <span style="color:#000000;"> } </span><span style="color:#0000FF;">else</span><span style="color:#000000;"> { </span><span style="color:#008080;">26</span> <span style="color:#000000;"> LOG.error(</span><span style="color:#000000;">"</span><span style="color:#000000;">Unknown data of type '</span><span style="color:#000000;">"</span><span style="color:#000000;">+</span><span style="color:#000000;"> requestXMLData.getClass() </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">'.</span><span style="color:#000000;">"</span><span style="color:#000000;"> ); </span><span style="color:#008080;">27</span> <span style="color:#000000;"> } </span><span style="color:#008080;">28</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> result; </span><span style="color:#008080;">29</span> <span style="color:#000000;">}</span></div> |
We make an implementation of the method getPrintString for a readable output of the XML.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">static</span><span style="color:#000000;"> String getPrintString(Node node) { </span><span style="color:#008080;"> 2</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">try</span><span style="color:#000000;"> { </span><span style="color:#008080;"> 3</span> <span style="color:#000000;"> DOMSource domSource </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> DOMSource(node); </span><span style="color:#008080;"> 4</span> <span style="color:#000000;"> StringWriter writer </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> StringWriter(); </span><span style="color:#008080;"> 5</span> <span style="color:#000000;"> StreamResult result </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> StreamResult(writer); </span><span style="color:#008080;"> 6</span> <span style="color:#000000;"> TransformerFactory tf </span><span style="color:#000000;">=</span><span style="color:#000000;"> TransformerFactory.newInstance(); </span><span style="color:#008080;"> 7</span> <span style="color:#000000;"> Transformer transformer </span><span style="color:#000000;">=</span><span style="color:#000000;"> tf.newTransformer(); </span><span style="color:#008080;"> 8</span> <span style="color:#000000;"> transformer.transform(domSource, result); </span><span style="color:#008080;"> 9</span> <span style="color:#000000;"> writer.flush(); </span><span style="color:#008080;">10</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> writer.toString(); </span><span style="color:#008080;">11</span> <span style="color:#000000;"> } </span><span style="color:#0000FF;">catch</span><span style="color:#000000;"> (TransformerException e) { </span><span style="color:#008080;">12</span> <span style="color:#000000;"> LOG.warn(</span><span style="color:#000000;">"</span><span style="color:#000000;">Unable to convert XML-Node '</span><span style="color:#000000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> node.toString() </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">' of class '</span><span style="color:#000000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> node.getClass() </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">' to string representation.</span><span style="color:#000000;">"</span><span style="color:#000000;">, e); </span><span style="color:#008080;">13</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#000000;">"</span><span style="color:#000000;">[non printable xml]</span><span style="color:#000000;">"</span><span style="color:#000000;">; </span><span style="color:#008080;">14</span> <span style="color:#000000;"> } </span><span style="color:#008080;">15</span> <span style="color:#000000;">}</span></div> |
And finally the method doSomething processing the xml element and returning an XmlObject needs to be implemented.
Bernhard Mähr @ OPITZ-CONSULTING published at http://thecattlecrew.wordpress.com/