springweb-servicessoapjax-wssaaj

Creating a SOAP using javax.xml.soap


I have been trying to write a SOAP web service using javax.xml.soap but the output I am getting is not as I am expecting.

my expected output is as:

<test:testment
    xmlns:test="http://www.shopper.com/schemas/CMS_Generic/testment_Request.xsd"
    xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" 
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <test:RequestHeader>
        <test:MessageId>171004081257000_3106</test:MessageId>
        <test:MsgSource>MUTUALIND</test:MsgSource>
        <test:ClientCode>MUTUALIND</test:ClientCode>
        <test:BatchRefNmbr>171004081257000_3106</test:BatchRefNmbr>

    </test:RequestHeader>
    <test:InstrumentList>
        <test:instrument>
            <test:InstRefNo>171004081257000_3106</test:InstRefNo>
            <test:MyProdCode>NETtest</test:MyProdCode>
            <test:TxnAmnt>99.5</test:TxnAmnt>
            <test:AccountNo>650000173</test:AccountNo>
            <test:testmentDt>2017-10-04</test:testmentDt>
            <test:RecBrCd>BOFA0BG3978</test:RecBrCd>
            <test:BeneAcctNo>1234569874</test:BeneAcctNo>
            <test:BeneName>INDIA TEST TEST</test:BeneName>
            <test:BeneAddr1>IND</test:BeneAddr1>
            <test:city>IND</test:city>
            <test:InstDt>2017-10-04</test:InstDt>
            <test:testmentDtl1>Mumbai</test:testmentDtl1>
            <test:testmentDtl2>UNITED KINGDOM</test:testmentDtl2>
            <test:EnrichmentSet>
                <test:Enrichment>TEST
                    CLIENT~SAVING~TEST~09582650000173~FAMILY_MAINTENANCE~1234569874
                </test:Enrichment>
            </test:EnrichmentSet>
        </test:instrument>
    </test:InstrumentList>
</test:testment>

Solution

  • Try converting the SOAP into String and than use replaceAll of String with the text you want to get after that convert it back to SOAP

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    soapMessage.writeTo(out);
    
    //converting to String so that replacing certain soap tags could be easy
    String strMsg = new String(out.toByteArray());
    
    strMsg = strMsg.replaceAll("SOAP-ENV", "test");
    strMsg = strMsg.replaceAll("Header", "RequestHeader");
    strMsg = strMsg.replaceAll("Envelope", "testment");
    strMsg = strMsg.replaceAll("Body", "InstrumentList");
    strMsg = strMsg.replaceAll("xmlns:pay=\"http://www.w3.org/2003/05/soap-envelope\"",
            "xmlns:SOAP-ENV=\"http://www.w3.org/2003/05/soap-envelope\"");
    
    //converting String back to SOAP 
    InputStream is = new ByteArrayInputStream(strMsg.getBytes());
    soapMessage = MessageFactory.newInstance().createMessage(null, is);
    
    soapMessage.saveChanges();
    soapMessage.writeTo(System.out);
    return soapMessage;