Java 生成 SOAP 信封
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28570086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Generate SOAP Envelope
提问by simplo
i have the following method :
我有以下方法:
String[] getEmployeeDetails ( int employeeNumber ); The assosiate request look like this :
String[] getEmployeeDetails ( int employeeNumber ); 助理请求如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:getEmployeeDetails
xmlns:ns1="urn:MySoapServices">
<param1 xsi:type="xsd:int">1016577</param1>
</ns1:getEmployeeDetails>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
this example come from this link [http://www.soapuser.com/basics3.html][1]
这个例子来自这个链接 [ http://www.soapuser.com/basics3.html][1]
I don't understand how they do to generate it programtically with java. Please help !
我不明白他们是如何用 java 以编程方式生成它的。请帮忙 !
采纳答案by Koitoer
Basically you need to use SAAJ API, this is an API that use SOAPMessage and give you some objects and methods to create SOAP request programatically, you shouls see thislink for further reference. Also review the documentationfrom Oracle, they give you some useful examples. For real example you could check this link
基本上您需要使用 SAAJ API,这是一个使用 SOAPMessage 的 API,并为您提供一些对象和方法来以编程方式创建 SOAP 请求,您应该查看此链接以供进一步参考。还要查看Oracle的文档,它们为您提供了一些有用的示例。例如,您可以查看此链接
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
// Retrieve different parts
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
// Two ways to extract headers
SOAPHeader soapHeader = soapEnvelope.getHeader();
soapHeader = soapMessage.getSOAPHeader();
// Two ways to extract body
SOAPBody soapBody = soapEnvelope.getBody();
soapBody = soapMessage.getSOAPBody();
// To add some element
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name bodyName = soapFactory.createName("getEmployeeDetails","ns1","urn:MySoapServices");
SOAPBodyElement purchaseLineItems = soapBody.addBodyElement(bodyName);
Name childName = soapFactory.createName("param1");
SOAPElement order = purchaseLineItems.addChildElement(childName);
order.addTextNode("1016577");
回答by Hubschrauber
You could grab the wsdl from the soap service (usually something like http://endpointurl?wsdl), and then use Apache CXF's wsdl2java utility to generate code with the -client parameter. The generated code will do a lot of the work for you in terms of building a valid SOAP request and sending it to the endpoint, or if you just want to see how it works, you could follow the calls it makes into the CXF source and see how they're doing things.
您可以从soap 服务(通常类似于http://endpointurl?wsdl)获取wsdl,然后使用Apache CXF 的wsdl2java 实用程序生成带有-client 参数的代码。生成的代码将在构建有效的 SOAP 请求并将其发送到端点方面为您做很多工作,或者如果您只是想看看它是如何工作的,您可以按照它对 CXF 源进行的调用和看看他们是如何做事的。