[英]Soap client : SAAJ0514 unable to create envelope from given source because the root element is not named Envelope
[英]Use default namespace in valid SOAP SAAJ envelope
這兩條肥皂信息有效嗎? 不同的部分是命名空間屬性xmlns =“http://www.sigvalue.com/acc”。 第一個soap是一個樣本,第二個是由java代碼生成的,用於生成相同的soap消息。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetNGPList xmlns="http://www.sigvalue.com/acc">
<UserData>
<senderId>string</senderId>
<channelId>string</channelId>
<timeStamp>dateTime</timeStamp>
</UserData>
<zipCode>string</zipCode>
</GetNGPList>
</soap:Body>
</soap:Envelope>
。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetNGPList>
<UserData xmlns="http://www.sigvalue.com/acc">
<senderId>string</senderId>
<channelId>string</channelId>
<timeStamp>dateTime</timeStamp>
</UserData xmlns="http://www.sigvalue.com/acc">
<zipCode>string</zipCode>
</GetNGPList>
</soap:Body>
</soap:Envelope>
如果第二個肥皂無效,我怎么能和第一個肥皂一樣? GetNGPList.addNamespaceDeclaration(“xmlns”,“ http://www.sigvalue.com/acc ”); 這條線不能按預期工作。 這是JAVA代碼:
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsi", gXSIServerURI);
envelope.addNamespaceDeclaration("xsd", gXSDServerURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement GetNGPList = soapBody.addChildElement("GetNGPList");
GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc");
SOAPElement UserData = GetNGPList.addChildElement("UserData");
SOAPElement senderId = UserData.addChildElement("senderId");
SOAPElement channelId = UserData.addChildElement("channelId");
SOAPElement timeStamp = UserData.addChildElement("timeStamp");
senderId.addTextNode("string");
channelId.addTextNode("string");
timeStamp.addTextNode("dateTime");
SOAPElement zipCode = GetNGPList.addChildElement("zipCode");
zipCode.addTextNode("string");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "sample");
soapMessage.saveChanges();
/* Print the request message */
//System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
要將名稱空間設置為默認名稱空間 ,只需使用空字符串( ""
)作為前綴名稱 :
SOAPElement GetNGPList =
soapBody.addChildElement("GetNGPList", "", "http://www.sigvalue.com/acc");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
上面的代碼將xmlns="http://www.sigvalue.com/acc"
應用於GetNGPList
。
你的代碼改編:
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement GetNGPList =
soapBody.addChildElement("GetNGPList", "", "http://www.sigvalue.com/acc");
SOAPElement UserData = GetNGPList.addChildElement("UserData");
...
像往常一樣,當您在addChildElement()
省略命名空間前綴聲明時,子節點從其父節點繼承其命名空間。
上面的代碼將生成您需要的內容:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<GetNGPList xmlns="http://www.sigvalue.com/acc">
<UserData>
<senderId>string</senderId>
...
你可以刪除這一行:
GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc");
並添加此行
envelope.addNamespaceDeclaration("","http://www.sigvalue.com/acc");
幾點說明:
我建議使用框架來處理Web服務,Apache CXF可以為您做很多棘手的工作: http : //cxf.apache.org/
確保遵循正常的命名標准,這使得更容易閱讀代碼。 變量GetNGPList
應該是getNGPList
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.