我有以下方法:
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>
此示例来自此链接[http://www.soapuser.com/basics3.html][1]
我不明白他们如何使用java以编程方式生成它。 请帮忙!
答案 0 :(得分:8)
基本上你需要使用SAAJ API,这是一个使用SOAPMessage的API,并为您提供一些以编程方式创建SOAP请求的对象和方法,您可以看到this链接以供进一步参考。另请查看Oracle的documentation,它们会为您提供一些有用的示例。对于真实示例,您可以查看此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");
答案 1 :(得分:0)
您可以从soap服务中获取wsdl(通常类似于http://endpointurl?wsdl),然后使用Apache CXF的wsdl2java实用程序生成带有-client参数的代码。生成的代码将在构建有效的SOAP请求并将其发送到端点方面为您完成大量工作,或者如果您只是想了解它是如何工作的,您可以按照它对CXF源的调用进行操作,看看他们是如何做的。