2017-06-23 29 views
0
how to add <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/"> in xml soap request. 

我的示例請求中給出below.I創建JAXB註解的類和編組對象爲XML格式,但是我需要添加上述肥皂envlope和身體在向服務器發送請求之前請求。如何添加<皁:信封>和<soap:body>在XML請求

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<StatusRequest> 
<AccountID>231</AccountID> 
<PassPhrase>sddddd</PassPhrase> 
<StatusList> 
<PICNumber>111111</PICNumber> 
</StatusList> 
<Test>Y</Test> 
</StatusRequest> 

請提供示例程序。

+1

您是否使用任何WS API?像Axis/Axis2/CXF/JAX-WS一樣? –

回答

1

使用javax.xml.soap。

您需要從要放入信封內的對象(在此示例中用JAXB編組它)中獲取Document,並將其放入正文中。

這樣:

MessageFactory mfactory = MessageFactory.newInstance(); 
SOAPMessage soapMessage = mfactory.createMessage(); 
SOAPBody soapBody = petition.getSOAPBody(); 
soapBody.addDocument(marshaller.marshallDoc(obj)); 
soapMessage.saveChanges(); 

這樣,當你這樣做:

soapMessage.writeTo(System.out); 

你會看到在輸出SOAP部分。

0
SOAPPart soapPart = message.getSOAPPart(); 
// Obtain SOAP Part 

SOAPEnvelope envelope = soapPart.getEnvelope(); 
// Obtain Envelope from SOAP Part 

SOAPHeader header = envelope.getHeader(); 
// Obtain Header from Envelope 

SOAPBody body = envelope.getBody(); 
// Obtain Body from Envelope 

QName headerName = new QName("namespaceURI", "localPart"); 
// SOAPHeaderElement must have an associated QName object. 

SOAPHeaderElement headerElement = header.addHeaderElement(headerName); 
// Create new SOAPHeaderElement object initialized with the specified Qname 
// and add it to this SOAPHeader object. 

headerElement.addAttribute(new QName("localPart"), "valueToAdd"); 
// Add attribute to header 

QName bodyName = new QName("namespaceURI", "localPart"); 
// SOAPBodyElement must have an associated QName object. 

SOAPBodyElement bodyElement = body.addBodyElement(bodyName); 
// Add Body Element 

您可能會這tutorial和相應的JavaDocs爲SAAJ。

相關問題