[英]Creation SOAP message request in JAVA
我想這樣在JAVA中創建SOAP消息請求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:WSFS">
<soapenv:Header/>
<soapenv:Body>
<urn:saveSignedPDF>
<docID>??</docID>
<input>??</input>
</urn:saveSignedPDF>
</soapenv:Body>
有人可以幫我嗎? 謝謝
我的代碼:
String url = "http://mydomain/scripts/ws4.php";
SOAPMessage soapResponse = soapConnection.call(
createSOAPRequest(documentId, encoded), url);
private static SOAPMessage createSOAPRequest(String documentId,
String fileToUpdate) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "WSFS";
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody soapBody = envelope.getBody();
SOAPBodyElement element = soapBody.addBodyElement(envelope.createName("saveSignedPDF", "urn", ""));
element.addChildElement("docID").addTextNode(documentId);
element.addChildElement("input").addTextNode(fileToUpdate);
soapMessage.saveChanges();
return soapMessage;
}
jaxws-maven-plugin可以從提供的wsdl生成所需的類。
Maven插件: https : //jax-ws-commons.java.net/jaxws-maven-plugin/
范例: http : //www.hascode.com/2010/03/how-to-build-a-confluence-soap-client-in-5-minutes/
您可以使用jaxb2-maven-plugin從WSDL生成SOAP請求消息。
嗨,如果您想在信封中添加xmlns:urn="urn:WSFS"
命名空間,則應在代碼中添加一行:
private static SOAPMessage createSOAPRequest(String documentId,
String fileToUpdate) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "urn:WSFS"; // change form "WSFS" to "urn:WSFS"
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("urn", serverURI); // this line will add namespece in your envelope
...
更改之后,您的請求將如下所示:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:WSFS">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<urn:saveSignedPDF>
<docID>1</docID>
<input>string</input>
</urn:saveSignedPDF>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如果要將默認名稱空間從SOAP-ENV
更改為soapenv
請閱讀此問題和答案 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.