Java 生成 SOAP 信封
Java Generate SOAP Envelope
我有以下方法:
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>
这个例子来自这个link[http://www.soapuser.com/basics3.html][1]
我不明白他们如何使用 java 以编程方式生成它。
请帮忙!
您可以从 soap 服务获取 wsdl(通常类似于 http://endpointurl?wsdl),然后使用 Apache CXF 的 wsdl2java 实用程序生成带有 -client 参数的代码。生成的代码将在构建有效的 SOAP 请求并将其发送到端点方面为您做很多工作,或者如果您只是想看看它是如何工作的,您可以关注它对 CXF 源代码的调用,以及看看他们是怎么做的。
基本上您需要使用 SAAJ API,这是一个使用 SOAPMessage 并为您提供一些对象和方法以编程方式创建 SOAP 请求的 API,您应该看到 this link for further reference. Also review the documentation from Oracle, they give you some useful examples. For real example you could check this link
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");
我有以下方法:
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>
这个例子来自这个link[http://www.soapuser.com/basics3.html][1]
我不明白他们如何使用 java 以编程方式生成它。 请帮忙!
您可以从 soap 服务获取 wsdl(通常类似于 http://endpointurl?wsdl),然后使用 Apache CXF 的 wsdl2java 实用程序生成带有 -client 参数的代码。生成的代码将在构建有效的 SOAP 请求并将其发送到端点方面为您做很多工作,或者如果您只是想看看它是如何工作的,您可以关注它对 CXF 源代码的调用,以及看看他们是怎么做的。
基本上您需要使用 SAAJ API,这是一个使用 SOAPMessage 并为您提供一些对象和方法以编程方式创建 SOAP 请求的 API,您应该看到 this link for further reference. Also review the documentation from Oracle, they give you some useful examples. For real example you could check this link
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");