1

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;
}
Stepan
  • 97
  • 2
  • 11

3 Answers3

1

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/

0

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

Bhokal
  • 71
  • 3
0

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.

Community
  • 1
  • 1
Mateusz Sroka
  • 2,255
  • 2
  • 17
  • 19
  • I am desperate... I tried just about every combination, but no success. I'm getting error at `soapMessage.saveChanges();` due to my log report. Can you help me with that @MateuszSroka? – Stepan Jan 18 '16 at 15:19
  • The error is `Error : com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message` Full log record here http://pastebin.com/XsjCjpt0 – Stepan Jan 19 '16 at 09:24
  • Ok, but I need full stack not the first line. – Mateusz Sroka Jan 19 '16 at 09:27
  • Full log record here pastebin.com/XsjCjpt0 – Stepan Jan 19 '16 at 09:35
  • 1
    You have problem with TreeWalker class `Caused by: java.lang.ClassNotFoundException: org.apache.xml.serializer.TreeWalker` this is mentioned lower in stack trace. You need to get serializer-2.7.1.jar which contains this class and simply put it to tomcat/lib and restart the the tomcat application server – Mateusz Sroka Jan 19 '16 at 09:39