简体   繁体   中英

How to send SOAP Request in JAVA

I am very new to using SOAP services so I would appreciate any help. I am trying to send a SOAPMessage request using SAAJ.

Here is an example of a request I am trying to send.

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sms="http://example.com/SMX/example">
   <soap:Header>
      <sms:AuthHeader>
         <sms:UserName>Username</sms:UserName>
<sms:Password>Password</sms:Password>
      </sms:AuthHeader>
   </soap:Header>
   <soap:Body>
      <sms:SmsStdCreate>
         <!--Optional:-->
         <sms:requestList>
            <!--Zero or more repetitions:-->
            <UpdateList>
               <PersonParams>
                  <!--Optional:-->
                  <ssn>3993202</ssn>
                  <firstName>testFromXML</firstName>
                  <middleName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <lastName>TESTXML</lastName>
                  <birthDate>1992-03-10-05:00</birthDate>
                  <birthCity xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <birthState>VA</birthState>
                  <birthCountry>US</birthCountry>
                  <isDualCitizenship>No</isDualCitizenship>
                  <gender>G</gender>
               </PersonParams>
            </UpdateList>
         </sms:requestList>
      </sms:SmsStdCreate>
   </soap:Body>
</soap:Envelope>

Here's an example payload of the Soap Client I am trying to implement. I have an JAVA String of the XML SOAP Request that I wanted to put in the SoapBody so I wouldn't have to configure every single XML element since there are atleast 50 fields in the request. Would there be a way to pass that string into the body?

public class SAAJSoapClient {

    @Autowired
    SMSStdCreateProcessorService smsStdCreateProcessorServices;

    String soapEndpointUrl = "http://example.com/SMX/example";
    String soapAction = "http://example.com/SMX/example/SmsStdCreate";

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


        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("soap", "http://example.com/SMX/example");

        SOAPBody soapBody = envelope.getBody();

    }

    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 soapRequest = createSOAPRequest(soapAction);
            SOAPMessage soapResponse = soapConnection.call(soapRequest, soapEndpointUrl);
            // Print the SOAP Response
            System.out.println("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            System.out.println();
        } catch (Exception e) {
            System.err.println("\nError occurred while sending SOAP Request to Server");
            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
        System.out.println("Request SOAP Message");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }
}

Convert your soap request to org.w3c.dom.Document and:

document.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Body")
                            .item(0).getFirstChild().setTextContent("your data");

Then you should convert the document to a SOAPMessage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM