我正在尝试将SOAP调用集成到我的Java应用程序中。我已经在线跟踪了各种教程,但是当我运行应用程序时,它会在sm.saveChanges()
处出错,如果该行被注释掉,则会在sm.writeTo(System.out)
处给出错误。
以下是代码:
//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!");
相应的错误是:
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
有什么建议吗?
答案 0 :(得分:3)
你真的不想亲手做到这一点。生成Web服务客户端并使用该客户端。
您可以使用Apache Axis WSDL2Code。还有其他选项可以生成客户端。例如。 Eclipse有built in tool这样做。
答案 1 :(得分:0)
同意Udo Held和其他人的意见。只是想提一下,如果你真的想要在如此低级别上集成SOAP调用,你可以先使用Apache Axis进行调查,而不是使用调试和http数据包跟踪工具进行一些调查,以找出你错过的内容。这种方式值得在某些特定情况下遇到麻烦,例如,您尝试使用XPath从SOAP响应中提取单个节点,而不是首先将其绑定到POJO对象。
答案 2 :(得分:0)
正如@Udo Held所提到的,你不想手工构建SOAP请求。
请参阅此问题的答案:How to get response from SOAP?
您可以使用wsimport
工具自动生成连接SOAP服务所需的所有代码。
wsimport
附带JDK,因此您已经在系统上安装了它(没有额外的下载)并且假设Java在您的路径上,您可以转到任何终端并输入:
wsimport http://www.url.to.wsdl.asmx?wsdl -p com.whateveruwant -Xnocompile -d . -keep
这样做,您将能够非常简单地使用自动生成的代码调用Web服务,例如:
CustomInterface soap = new CustomEndpoint().getCustomInterface();
System.out.println(soap.getAnswerFromWs("ParamValue"));