2017-06-23 28 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。

相关问题