【问题标题】:Java Generate SOAP EnvelopeJava 生成 SOAP 信封
【发布时间】:2015-02-17 20:00:51
【问题描述】:

我有以下方法:

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 以编程方式生成它。 请帮忙!

【问题讨论】:

    标签: java soap


    【解决方案1】:

    基本上您需要使用 SAAJ API,这是一个使用 SOAPMessage 并为您提供一些对象和方法来以编程方式创建 SOAP 请求的 API,您应该查看 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");
    

    【讨论】:

    • 感谢您的回答,我会尝试这种方法,我认为它符合我的需要
    • 嗨!我试了一下,我可以正确地创建我的 SoapMessage。现在我想为
    • javax.xml.soap.Name 这是正确的名称类吗?来自 saaj.jar
    【解决方案2】:

    您可以从soap 服务中获取wsdl(通常类似于http://endpointurl?wsdl),然后使用Apache CXF 的wsdl2java 实用程序生成带有-client 参数的代码。生成的代码将在构建有效的 SOAP 请求并将其发送到端点方面为您完成大量工作,或者如果您只是想看看它是如何工作的,您可以按照它对 CXF 源的调用和看看他们是怎么做的。

    http://cxf.apache.org/docs/how-do-i-develop-a-client.html

    【讨论】:

    • 感谢您的回答,但我没有 wsdl 文件。我会尝试 Koitoer 的方法
    • 如果您调用的 SOAP 服务发布在应用服务器上,通常情况下,您只需将“?wsdl”添加到您的基本端点 url 的末尾即可下载 wsdl用于调用服务。仅供参考,以防您错过了下载 wsdl 副本并避免进行大量手动编码工作的机会。
    • 是的,我知道,但是这个 SOAP 服务没有 wsdl,所以我尝试自己构建它以在 Http 请求中发送它。现在我需要知道如何为 xsi:type="xsd:int 这样的命名空间设置约束。我搜索它,发现我必须使用 XMLType_INT 之类的东西。你能告诉我请使用它吗?谢谢帮助
    猜你喜欢
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多