简体   繁体   English

如何添加<soap:Envelope>和<soap:body>在 xml 请求中

[英]how to add <soap:Envelope> and <soap:body> in xml request

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.

My sample request is given below.I created jaxb annotated classes and marshalled the object to xml format ,but i need to add above soap envlope and body in request before sending request to server.下面给出了我的示例请求。我创建了 jaxb 注释类并将对象编组为 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>

Please provide sample program.请提供示例程序。

Use a javax.xml.soap.使用 javax.xml.soap。

You need to get a Document from the object you want to put inside the envelope (marshal it with JAXB, in the example) an put it in the body.您需要从要放入信封的对象中获取一个 Document(在示例中使用 JAXB 对其进行编组)并将其放入正文中。

This way:这样:

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

This way when you do:当你这样做时:

soapMessage.writeTo(System.out);

You will see SOAP part in the output.您将在输出中看到 SOAP 部分。

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

You might this tutorial and corresponding JavaDocs for SAAJ.您可以使用本教程和 SAAJ 的相应 JavaDocs。

Assume that req is a class marked by javax.xml.bind.annotation , then:假设req是一个由javax.xml.bind.annotation标记的类,那么:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
marshaller.marshal(req, doc);

MessageFactory mfactory = MessageFactory.newInstance();
SOAPMessage soapMessage = mfactory.createMessage();
SOAPBody soapBody = soapMessage.getSOAPBody();
soapBody.addDocument(doc);

var baos = new ByteArrayOutputStream();
soapMessage.writeTo(baos);
var str = new String(baos.toByteArray(), Charset.forName("UTF8"));
log.info("SOAPMessage: {}", str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM