Spring Boot 如何将xmlns:tns=”http://schemas.xmlsoap.org/soap/encoding/“添加到soap请求

hkmswyz6  于 2023-10-16  发布在  Spring
关注(0)|答案(1)|浏览(96)

我写了一个调用soap服务的客户端,但它发送的请求不是我所期望的。
下面是我代码:

public MessageResponse traGiayToHoSo (MessageRequest request) {
        Message message = new Message();
        ThongTinXacThuc thongTinXacThuc = new ThongTinXacThuc();
        ThongTinHoSo thongTinHoSo = new ThongTinHoSo();
        FileDinhKem fileDinhKem = new FileDinhKem();

        fileDinhKem.setFileName("FvutqPDmkkC4cWjRsample.docx");
        fileDinhKem.setExtension("docx");
        fileDinhKem.setByteFile("UEsDBBQABgAIAAAAIQACD0AObDN+AHb0BC9/49I0+Ucq8qbBPyFxyfgjbnRFHdZGrU5P8rUqfPjZtgexsaAAAAAAAAAAAAAzkMAAGN1c3RvbVhtbC9fcmVscy9pdGVtMy54bWwucmVsc1BLBQYAAAAAFgAWALkFAADVRQAAAAA=");
        fileDinhKem.setSize("19364");

        thongTinXacThuc.setUserName("igate_eoffice");
        thongTinXacThuc.setPassWord("igate_eoffice");

        thongTinHoSo.setFileDinhKems(new FileDinhKem[]{fileDinhKem});
        thongTinHoSo.setMaHoSo("587205");

        message.setThongTinHoSo(thongTinHoSo);
        message.setThongTinXacThuc(thongTinXacThuc);

        request.setMessage(message);

        MessageResponse response = (MessageResponse) getWebServiceTemplate().marshalSendAndReceive(request);
        System.out.println(response.toString());
        return response;
    }

使用上面的代码,我发送一个请求如下:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <messageRequest>
            <message>
                <thongTinHoSo>
                    <fileDinhKems>
                        <byteFile>UEsDBBQABgAIAAAAIQACD0AObDN+AHb0BC9/49I0+Ucq8qbBPyFxyfgjbnRFHdZGrU5P8rUqfPjZtgexsaAAAAAAAAAAAAAzkMAAGN1c3RvbVhtbC9fcmVscy9pdGVtMy54bWwucmVsc1BLBQYAAAAAFgAWALkFAADVRQAAAAA=</byteFile>
                        <extension>docx</extension>
                        <fileName>FvutqPDmkkC4cWjRsample.docx</fileName>
                        <size>19364</size>
                    </fileDinhKems>
                    <maHoSo>587205</maHoSo>
                </thongTinHoSo>
                <thongTinXacThuc>
                    <passWord>igate_eoffice</passWord>
                    <userName>igate_eoffice</userName>
                </thongTinXacThuc>
            </message>
        </messageRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我想将xmlns:tns=”http://schemas.xmlsoap.org/soap/encoding/“添加到SOAP-ENV:Envelope标记中,我应该怎么做?

atmip9wb

atmip9wb1#

尝试使用以下方法:

import org.springframework.ws.soap.SoapEnvelope;
import org.springframework.ws.soap.saaj.SaajSoapMessage;

public MessageResponse traGiayToHoSo (MessageRequest request) {
    // ... Your existing code ...

    // Create a SaajSoapMessage
    SaajSoapMessage saajSoapMessage = (SaajSoapMessage) getWebServiceTemplate().marshalSendAndReceive(request);

    // Get the SoapEnvelope
    SoapEnvelope soapEnvelope = saajSoapMessage.getEnvelope();

    // Add the xmlns:tns namespace declaration
    soapEnvelope.addNamespaceDeclaration("tns", "http://schemas.xmlsoap.org/soap/encoding/");

    // Print the modified SOAP request
    System.out.println(saajSoapMessage.toString());

    // Continue with your logic
    MessageResponse response = (MessageResponse) saajSoapMessage.getPayloadSource();

    return response;
}

你可以修改你的代码来添加xmlns:tns命名空间声明,就像我发送的那样。

相关问题