Stepan
Stepan

Reputation: 97

Creation SOAP message request in JAVA

I want to create SOAP message request in JAVA like this:

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

Could some one help me do this? Thank you

My code:

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

Upvotes: 1

Views: 2237

Answers (3)

Mateusz Sroka
Mateusz Sroka

Reputation: 2335

Hi if you want to add xmlns:urn="urn:WSFS" namespace in your envelope you should add one line to your code:

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

  ...

After this change your request will look like this:

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

If you want to change default namespace from SOAP-ENV to soapenv please read this question and answer.

Upvotes: 0

user5789865
user5789865

Reputation:

jaxws-maven-plugin can generate classes you need from wsdl you provide.

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

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

Upvotes: 1

Bhokal
Bhokal

Reputation: 71

You can use jaxb2-maven-plugin to generate SOAP request message from WSDL.

Upvotes: 0

Related Questions