0

I am trying to integrate SOAP calls into my Java application. I have followed various tutorials online, however when I run the application it gives errors at sm.saveChanges() and if that line is commented out it gives the errors at sm.writeTo(System.out).

Below is the code:

//Create a SOAPConnection   
          SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
          SOAPConnection connection = sfc.createConnection();

          MessageFactory mf = MessageFactory.newInstance();
          SOAPMessage sm = mf.createMessage();
          SOAPPart sp = sm.getSOAPPart();
          SOAPEnvelope sv = sp.getEnvelope();

          SOAPHeader sh = sm.getSOAPHeader();
          //SOAPHeaderElement headerElement = sh.addHeaderElement(sv.createName("Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

          SOAPBody sb = sm.getSOAPBody();

          //sh.detachNode();


          MimeHeaders headers = sm.getMimeHeaders();
          headers.addHeader("SOAPAction", "\"\""); 
          //QName bodyName = new QName("http://quoteCompany.com", "GetQuote", "d");
          //QName bodyName = new QName(msisdn, "GetRioInformationsRequest", "ser");
          //QName bodyName = new QName("http://schemas.xmlsoap.org/soap/encoding/", "GetRioInformationsRequest", XMLConstants.DEFAULT_NS_PREFIX);
          QName bodyName = new QName("http://quoteCompany.com/", "GetQuote", "ser");
          //QName bodyName = sv.createQName("http://quoteCompany.com/", "GetQuote", "ser");
          Name  bodyName2 = sv.createName("GetQuote","ser", "http://quoteCompany.com/");
          SOAPBodyElement bodyElement = sb.addBodyElement(bodyName2);
          //QName qn = new QName("aName");
          //Name qn = sv.createName("rioSearchRequest");
          QName qn = new QName("returnCode");

          SOAPElement quotation = bodyElement.addChildElement(qn);
          //quotation.addChildElement(qn2);

          //Name qn2 = sv.createName("msisdn");
          //qn2.
          //quotation.addAttribute(qn2, msisdn);
          //quotation.addTextNode("TextMode");
          quotation.addTextNode(msisdn);

          System.out.println("Saving SOAP message.");
          sm.saveChanges();
          System.out.println("\n Soap Request:\n");
          sm.writeTo(System.out);
          System.out.println("Request Outputted for viewing.");

          //URL endpoint = new URL("http://yourServer.com");
          URL endpoint = new URL("http://127.0.0.1:8088/mockSoapBinding");
          System.out.println("URL entered.");
          SOAPMessage response = connection.call(sm, endpoint);
          System.out.println("Resquest Sent!");
          System.out.println(response.getContentDescription());
          System.out.println("Response obtained!");

And the corresponding errors are:

    XSL-1101: (Fatal Error) DOMSource node as this type not supported.
Jun 27, 2012 4:37:45 PM com.sun.xml.messaging.saaj.soap.MessageImpl saveChanges
SEVERE: SAAJ0539: Unable to get header stream in saveChanges
Jun 27, 2012 4:37:45 PM com.sun.xml.messaging.saaj.soap.MessageImpl saveChanges
SEVERE: SAAJ0540: Error during saving a multipart message
SOAP Call Issue: Error during saving a multipart message
com.sun.xml.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message
    at com.sun.xml.messaging.saaj.soap.MessageImpl.saveChanges(MessageImpl.java:1209)
    at com.application.package.SOAPCalls.SOAPCall(SOAPCalls.java:66)
    at com.application.package.Main.<init>(Main.java:97)
    at com.application.package.Main.main(Main.java:253)
Caused by: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Unable to get header stream in saveChanges: 
    at com.sun.xml.messaging.saaj.soap.MessageImpl.saveChanges(MessageImpl.java:1179)
    ... 3 more
Caused by: java.io.IOException: XSL-1101: (Fatal Error) DOMSource node as this type not supported.
    at com.sun.xml.messaging.saaj.soap.impl.EnvelopeImpl.output(EnvelopeImpl.java:306)
    at com.sun.xml.messaging.saaj.soap.impl.EnvelopeImpl.output(EnvelopeImpl.java:317)
    at com.sun.xml.messaging.saaj.soap.SOAPPartImpl.getContentAsStream(SOAPPartImpl.java:324)
    at com.sun.xml.messaging.saaj.soap.MessageImpl.getHeaderBytes(MessageImpl.java:1020)
    at com.sun.xml.messaging.saaj.soap.MessageImpl.saveChanges(MessageImpl.java:1171)
    ... 3 more

Any suggestions?

Udo Held
  • 12,314
  • 11
  • 67
  • 93
miller2j
  • 79
  • 2
  • 7
  • Is there are reason you don't want to use one of the existing SOAP libraries out there like Apache Axis? – Drizzt321 Jun 27 '12 at 22:05
  • Check [this](http://blog.aplikacja.info/2010/06/configurationexception-xml-22101-fatal-error-domsource-node-as-this-type-not-supported/) link as well. – Jeshurun Jun 27 '12 at 22:09

3 Answers3

3

You don't really want to do this by hand. Generate a web service client and use that one.

You could use Apache Axis WSDL2Code. There are other options to generate a client as well. E.g. Eclipse has a built in tool for doing that.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
0

Agreed with Udo Held and others. Just want to mention if you really want to integrate SOAP calls on such a low level you can first do it with Apache Axis than do some investigation using debug and http packets trace tools to find out what you missed. This way worth the troubles for some specific cases when you for example are trying to pull out individual nodes from SOAP response using XPath without binding it to POJO objects at first.

Viktor Stolbin
  • 2,899
  • 4
  • 32
  • 53
0

As @Udo Held mentions, you don't want to construct SOAP requests by hand.

Please see the answer to this question: How to get response from SOAP?

You can use the wsimport tool to auto-generate all the code you need for connecting to the SOAP service.

wsimport ships with the JDK, so you already have it on your system (no additional downloads) and assuming Java is on your path, you can go to any terminal and type:

wsimport http://www.url.to.wsdl.asmx?wsdl -p com.whateveruwant -Xnocompile -d . -keep

This being done, you'll be able to invoke the web service with the auto-generated code quite simply, such as like:

CustomInterface soap = new CustomEndpoint().getCustomInterface();
System.out.println(soap.getAnswerFromWs("ParamValue"));
Community
  • 1
  • 1
Cuga
  • 17,668
  • 31
  • 111
  • 166