Link to home
Start Free TrialLog in
Avatar of ocean O
ocean OFlag for United States of America

asked on

how to change the namespace in soap response

HI, I am writing a soap web service.
The response that I got is starting with : <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">,
but it require to be : <env:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">.

I tried to add below line code in package-info.java, to replace the soap prefix with env, but it didn't work.
xmlns = {
            @javax.xml.bind.annotation.XmlNs(prefix="env", namespaceURI="http://schemas.xmlsoap.org/soap/envelope/")            
}

any inputs is welcome.

Thanks
Avatar of Juan Carlos
Juan Carlos
Flag of Peru image

but it require to be : <env:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">. 
Change xlmlns  too:

env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">. 

Open in new window

sorry I thought you were generating the string manually.
The server who sends you this

<env:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

needs to change their XML and define a namespace "env".
Avatar of ocean O

ASKER

I can't change the server . I am thinking to write a soap handler  to change the namespace of response from <soap> to <env>. Something like below, but I don'y know where to call this method:
public boolean handleMessage(SOAPMessageContext smc) {

    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {
   
        try {
            SOAPMessage msg = smc.getMessage();
            SOAPPart sp = msg.getSOAPPart();

            sp.getEnvelope().removeNamespaceDeclaration("soap");

            sp.getEnvelope().setPrefix("ENV");
            sp.getEnvelope().getBody().setPrefix("ENV");
            sp.getEnvelope().getHeader().setPrefix("ENV");

            smc.setMessage(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
}
If the "env" namespace is not defined then it is invalid XML, you have to fix the server. Can you post the SOAP response you get?
but it require to be

The example that you show is not modifying the namespace but the namespace alias. The namespace in that example is identical in both. While I can't say for sure from only seeing one line, but that second example would not be what you want as you have defined the "soap" alias to point to a namespace, but you are using the "env" alias which probably points to nothing at all.

So I ask.. what is the bigger picture here? Why do you think that you need to modify this alias? If all parts of both the server and client are implemented correctly, the actual value of the alias shouldn't be of concern, just that they refer to a namespace, and THAT namespace has the correct value.
Avatar of ocean O

ASKER

I am migrating web services from weblogic to tomcat. I am using apache cxf for the implementation.
The response that I got is like this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <m:getWorkAreasResponse xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://netg.z.com/WS/AService/" xmlns:ns3="http://netg.z.com/WS/Common/">
         <m:net_areas>
            <m:net_Area>
               <m:net_area>west</m:net_area>
            </m:net_Area>
            <m:net_Area>
               <m:net_area>east</m:net_area>
            </m:net_Area>
         </m:net_areas>
      </m:getWorkAreasResponse>
   </soap:Body>
</soap:Envelope>

Open in new window


The original example response is like this:
<env:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <env:Body>
      <m:getWorkAreasResponse xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://netg.z.com/WS/AService/" xmlns:ns3="http://netgeo.vzw.com/WS/Common/">
         <m:net_areas>
            <m:net_Area>
               <m:net_area>west</m:net_area>
            </m:net_Area>
            <m:net_Area>
               <m:net_area>east</m:net_area>
            </m:net_Area>
         </m:net_areas>
      </m:getWorkAreasResponse>
   </env:Body>
</env:Envelope>

Open in new window


So I have to change all the "soap" namespace into "env" , like from "<soap:Envelope"  to "<env:Envelope".

Thanks
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ocean O

ASKER

mccarl:

Thanks for the info. I resolved the issue by adding :
<jaxws:properties>
                  <entry key="soap.env.ns.map">
                        <map>
                              <entry key="env" value="http://schemas.xmlsoap.org/soap/envelope/"/>
                        </map>
                  </entry>
            </jaxws:properties>