首页 > 解决方案 > 如何在 JAVA 中发送 SOAP 请求

问题描述

我对使用 SOAP 服务非常陌生,因此我将不胜感激。我正在尝试使用 SAAJ 发送 SOAPMessage 请求。

这是我尝试发送的请求示例。

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

这是我正在尝试实现的 Soap 客户端的示例负载。我有一个想要放入 SoapBody 的 XML SOAP 请求的 JAVA 字符串,因此我不必配置每个 XML 元素,因为请求中至少有 50 个字段。有没有办法将该字符串传递到身体中?

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

标签: javaspring-bootsoapsaaj

解决方案


将您的 soap 请求转换为 org.w3c.dom.Document 并:

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

然后您应该将文档转换为 SOAPMessage。


推荐阅读