如何修复Java Soap客户端有效负载?

时间:2019-01-18 15:19:50

标签: java soap client

我正在构建一个Java soap客户端来调用以前构建的Web服务。

Web服务可以完全正常运行,我已经使用soapUI对它进行了多次测试

客户端以某种方式工作,它调用服务,但没有任何响应。

已通过soap UI测试:

...

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webService.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:analyticsProcessService>
         <!--Optional:-->
         <arg0>Alin</arg0>
         <!--Optional:-->
         <arg1>sk</arg1>
         <!--Optional:-->
         <arg2>18-19-2019</arg2>
         <!--Optional:-->
         <arg3>V2</arg3>
         <!--Optional:-->
         <arg4>SQL Analytics</arg4>
      </web:analyticsProcessService>
   </soapenv:Body>
</soapenv:Envelope>

...

响应: ...

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:analyticsProcessServiceResponse xmlns:ns2="http://webService.com/">
         <return>Succes request has been proceed</return>
      </ns2:analyticsProcessServiceResponse>
   </S:Body>
</S:Envelope>
...

所以从这个角度来看还可以

肥皂客户端

...

package com.webService;

import javax.xml.soap.*;

public class SOAPClientSAAJ {

    // SAAJ - SOAP Client Testing
    public static void main(String args[]) {

        String soapEndpointUrl = "http://localhost:9898/analyticsSubmissionService?wsdl";
        String soapAction = "http://localhost:9898/analyticsSubmissionService/";
        callSoapWebService(soapEndpointUrl, soapAction);
    }

    private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String myNamespace = "web";
        String myNamespaceURI = "http://webService.com/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);


        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("analyticsProcessService", myNamespace);
        SOAPElement soapBodyElem0 = soapBodyElem.addChildElement("arg0", myNamespace);
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("arg1", myNamespace);
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("arg2", myNamespace);
        SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("arg3", myNamespace);
        SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("arg4", myNamespace);
        soapBodyElem0.addTextNode("Alin");
        soapBodyElem1.addTextNode("sk");
        soapBodyElem2.addTextNode("testare");
        soapBodyElem3.addTextNode("test");
        soapBodyElem4.addTextNode("test iar");
    }

    private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

            // Print the SOAP Response
            System.out.println("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            System.out.println();

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        createSoapEnvelope(soapMessage);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", soapAction);

        soapMessage.saveChanges();

        /* Print the request message, just for debugging purposes */
        System.out.println("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }
}

...

我得到控制台输出:

...

Request SOAP Message:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webService.com/"><SOAP-ENV:Header/><SOAP-ENV:Body><web:analyticsProcessService><web:arg0>Alin</web:arg0><web:arg1>sk</web:arg1><web:arg2>testare</web:arg2><web:arg3>test</web:arg3><web:arg4>test iar</web:arg4></web:analyticsProcessService></SOAP-ENV:Body></SOAP-ENV:Envelope>

    Response SOAP Message:
    <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:analyticsProcessServiceResponse xmlns:ns2="http://webService.com/"><return></return></ns2:analyticsProcessServiceResponse></S:Body></S:Envelope>
...

以某种方式,我认为我不匹配客户端中Web服务的XSD架构,例如:

<web:arg0>Alin</web:arg0>

vs soap UI一个

<arg0>Alin</arg0>

我需要帮助来解决此问题。

0 个答案:

没有答案