Search code examples
javasoapjaxbcxfjax-ws

Modify SOAP envelop to include Namespace


I have SOAP response of something like this

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
    xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
    <soapenv:Body>
        <calculate_response>
            <tid>33433</tid>
            <status>0</status>
            <reason>0</reason>
        </calculate_response>
    </soapenv:Body>
</soapenv:Envelope>

I need to add namespace to it and it should look something like this

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    xmlns:def="http://com.sample.xyz/Messages">
    <soapenv:Body>
        <def:calculate_response>
            <tid>33433</tid>
            <status>0</status>
            <reason>0</reason>
        </def:calculate_response>
    </soapenv:Body>
</soapenv:Envelope>

I have created stub using wsimport, and I need this response to be modified before processing it further.

Actually it's a two step process as I understood , first step is to add namespace to soap envelop then add prefix to it. Regarding prefix part I found this . I'm not clear about how to do first step.

Note: I need to modify response at client side and I cannot change SOAP at server side.


Solution

  • I have achieved this with the help of SOAP Message Handlers

    I've written handler which implements SOAPHandler<SOAPMessageContext> and overridden its handleMessage(SOAPMessageContext context){} with following implementation.

    //Get soapElement from your soap request.
    if (soapElement.getTagName().equalsIgnoreCase("calculate_response")) {
           soapElement.setElementQName(
             new QName("http://com.sample.xyz/Messages",
                      "calculate_response",
                      "def"));