We help IT Professionals succeed at work.

how to change the namespace in soap response

ocean O
ocean O asked
on
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
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT

Commented:
sorry I thought you were generating the string manually.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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".

Author

Commented:
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;
}
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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?
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.

Author

Commented:
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
IT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
I can't change the server .

Ok, I think I understand what you were trying to say here. I thought that you meant that you cant make any changes at all to the server code, and that you were trying to write a client and get the handler to modify the message as it receives it. But I believe now that you just meant that you can't change the underlying application server that your code is running on, i.e. it has to be Tomcat. But you can modify the code that is implementing the web service, yes? I'll continue assuming that to be the case.

The original example response is like this:

I still don't think this is correct. Is this a direct copy paste from the original server response? Or have you just manually edited a version from the current server to show what you mean? This XML shows an env prefix being used on the top-level element without a corresponding xmlns:env= definition of that prefix.

However, I do still think I understand what you are after, but I have never used Apache CXF and the solution will be specific to that, so I might not be much further help on that front. However, some basic googling has found entries like this...

http://cxf.547215.n5.nabble.com/How-to-customize-namespaces-position-and-prefix-in-CXF-response-td3423069.html

where it talks about including a piece of configuration like this...

<jaxws:endpoint ..... > 
     <jaxws:properties> 
            <entry key="soap.env.ns.map"> 
                  <map> 
                           <entry key="ns1" value="http://blah.blah"/> 
                           <entry key="ns2" value="http://blah.blah2"/> 

Open in new window


But I am not sure where this gets configured in CXF.



Finally, I am still keen to know why you want to do this at all. As stated before, whether the XML has env: or soap: as the prefix, it is still semantically identical and should not pose any problem to a correctly implemented client. Are you getting errors in the client that is consuming this web service? What language/environment is the client implemented in?

Author

Commented:
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>