我实现了一个soap客户端来调用第三方的Web服务方法。方法是:InsertData_Str
我的java应用程序出了问题。我需要向InsertData_Str方法添加xmlns属性,但它不起作用,它放一个空值,我不明白为什么。有什么想法吗?
以下是代码:
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
soapMessage.setContentDescription("MY Connector");
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://www.ik.com/ikConnect";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.setPrefix("soap");
SOAPBody soapBody = envelope.getBody();
SOAPElement soapMethod = soapBody.addChildElement("InsertData_Str"); //Method
//soapMethod.setAttribute("xmlns", "http://www.ik.com/ikConnect"); //This doesn't work
QName attributeName = new QName("xmlns");
soapMethod.addAttribute(attributeName,"http://www.ik.com/ikConnect"); //If I Debugg I can see that xmln attribute is OK but when the message is sent xmln is empty
这是输出:
<?xml version="1.0" encoding="utf-8" ?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><InsertData_Str xmlns=""><xdoc xmlns="http://www.ik.com/ikConnect">TEST</xdoc></InsertData_Str></SOAP-ENV:Body></soap:Envelope>
答案 0 :(得分:0)
我解决了。 我通过另一个addChildElement(String localName,String prefix,String uri)更改了SOAPElement addChildElement(String localName)方法
示例:
String serverURI = "http://www.ik.com/ikConnect";
SOAPElement soapMethod = soapBody.addChildElement("InsertData_Str", "", serverURI);