在JAVA中创建SOAP消息请求

时间:2016-01-18 11:40:00

标签: java soap request

我想在JAVA中创建SOAP消息请求,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"   xmlns:urn="urn:WSFS">
 <soapenv:Header/>
 <soapenv:Body>
    <urn:saveSignedPDF>
       <docID>??</docID>
       <input>??</input>
    </urn:saveSignedPDF>
 </soapenv:Body>

有人可以帮我这么做吗? 谢谢

我的代码:

String url = "http://mydomain/scripts/ws4.php";
SOAPMessage soapResponse = soapConnection.call(
createSOAPRequest(documentId, encoded), url);

private static SOAPMessage createSOAPRequest(String documentId,
        String fileToUpdate) throws Exception {     
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();      
    String serverURI = "WSFS";  
    SOAPEnvelope envelope = soapPart.getEnvelope();     
    SOAPBody soapBody = envelope.getBody();         
    SOAPBodyElement element = soapBody.addBodyElement(envelope.createName("saveSignedPDF", "urn", ""));     
    element.addChildElement("docID").addTextNode(documentId);
    element.addChildElement("input").addTextNode(fileToUpdate);
    soapMessage.saveChanges();
    return soapMessage;
}

3 个答案:

答案 0 :(得分:1)

jaxws-maven-plugin可以从您提供的wsdl生成所需的类。

Maven插件:https://jax-ws-commons.java.net/jaxws-maven-plugin/

示例:http://www.hascode.com/2010/03/how-to-build-a-confluence-soap-client-in-5-minutes/

答案 1 :(得分:0)

您可以使用jaxb2-maven-plugin从WSDL生成SOAP请求消息。

答案 2 :(得分:0)

如果您想在信封中添加xmlns:urn="urn:WSFS"命名空间,您应该在代码中添加一行:

private static SOAPMessage createSOAPRequest(String documentId,
    String fileToUpdate) throws Exception { 
  MessageFactory messageFactory = MessageFactory.newInstance();
  SOAPMessage soapMessage = messageFactory.createMessage();
  SOAPPart soapPart = soapMessage.getSOAPPart();
  String serverURI = "urn:WSFS";                      // change form "WSFS" to "urn:WSFS"
  SOAPEnvelope envelope = soapPart.getEnvelope();

  envelope.addNamespaceDeclaration("urn", serverURI); // this line will add namespece in your envelope

  ...

此更改后,您的请求将如下所示:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:WSFS">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <urn:saveSignedPDF>
            <docID>1</docID>
            <input>string</input>
        </urn:saveSignedPDF>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如果您想将默认命名空间从SOAP-ENV更改为soapenv,请阅读