如何在java中发送soap请求

vaj7vani  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(453)

我对使用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客户机的一个有效负载示例。我有一个xml soap请求的java字符串,我想把它放在soapbody中,这样就不必配置每个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;
    }
}
qyuhtwio

qyuhtwio1#

将soap请求转换为org.w3c.dom.document并:

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

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

相关问题