Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a namespace reference to a SOAP response with Apache Axis2 and WSDL2Java

I'm looking at the SOAP output from a web service I'm developing, and I noticed something curious:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <ns1:CreateEntityTypesResponse xmlns:ns1="http://somedomain.com/wsinterface">
         <newKeys>
            <value>1234</value>
         </newKeys>
         <newKeys>
            <value>2345</value>
         </newKeys>
         <newKeys>
            <value>3456</value>
         </newKeys>
         <newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
         <newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
         <errors>Error1</errors>
         <errors>Error2</errors>
      </ns1:CreateEntityTypesResponse>
   </soapenv:Body>
</soapenv:Envelope>

I have two newKeys elements that are nil, and both elements insert a namespace reference for xsi. I'd like to include that namespace in the soapenv:Envelope element so that the namespace reference is only sent once.

I am using WSDL2Java to generate the service skeleton, so I don't directly have access to the Axis2 API.

like image 376
user13224 Avatar asked Sep 16 '08 17:09

user13224


People also ask

What is namespace in SOAP request?

An XML namespace is a means of qualifying element and attribute names to disambiguate them from other names in the same document. This section provides a brief description of XML namespaces and how they are used in SOAP. For complete information, see http://www.w3.org/TR/REC-xml-names/

How do you consume Java SOAP Web services using Axis2 client?

IBM RAD provides direct facility to generate Webservice Client code from a wsdl file. All you need to do is create a new "Dynamic Web Service Project" in a RAD Workspace. Then import your wsdl file to this Project. Then, right click on that file, and select Webservices ---> Generate Client.

What is Apache Axis2 used for?

Apache Axis2/C can be used to provide and consume WebServices. It has been implemented with portability and ability to embed in mind, hence could be used as a Web services enabler in other software. Rampart is the security module of Axis2.

What is Axis2 server?

Apache Axis2™ is a Web Services JSON / SOAP / WSDL engine, the successor to the widely used Apache Axis SOAP stack. There are two implementations of the Apache Axis2 Web services engine - Apache Axis2/Java and Apache Axis2/C.


1 Answers

Using WSDL2Java

If you have used the Axis2 WSDL2Java tool you're kind of stuck with what it generates for you. However you can try to change the skeleton in this section:

   // create SOAP envelope with that payload
   org.apache.axiom.soap.SOAPEnvelope env = null;
   env = toEnvelope(
       getFactory(_operationClient.getOptions().getSoapVersionURI()),
       methodName,
       optimizeContent(new javax.xml.namespace.QName
       ("http://tempuri.org/","methodName")));

//adding SOAP soap_headers
_serviceClient.addHeadersToEnvelope(env);

To add the namespace to the envelope add these lines somewhere in there:

OMNamespace xsi = getFactory(_operationClient.getOptions().getSoapVersionURI()).
    createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");

env.declareNamespace(xsi);

Hand-coded

If you are "hand-coding" the service you might do something like this:

SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();   
SOAPEnvelope envelope = fac.getDefaultEnvelope();
OMNamespace xsi = fac.createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");

envelope.declareNamespace(xsi);
OMNamespace methodNs = fac.createOMNamespace("http://somedomain.com/wsinterface", "ns1");

OMElement method = fac.createOMElement("CreateEntityTypesResponse", methodNs);

//add the newkeys and errors as OMElements here...

Exposing service in aar

If you are creating a service inside an aar you may be able to influence the SOAP message produced by using the target namespace or schema namespace properties (see this article).

Hope that helps.

like image 89
Michael Sharek Avatar answered Oct 20 '22 01:10

Michael Sharek