使用javax.xml.soap创建SOAP

时间:2019-05-06 11:39:56

标签: spring web-services soap jax-ws saaj

我一直在尝试使用javax.xml.soap编写SOAP Web服务,但是我得到的输出与我期望的不一样。

我的预期输出是:

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

1 个答案:

答案 0 :(得分:0)

请尝试将SOAP转换为String,然后在将其转换回SOAP之后,使用StringAll的replaceAll和要获取的文本

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;