如何在不生成代码的情况下使用 WSDL 中完全填充的请求生成 SOAP 消息

发布于 2024-12-05 07:40:58 字数 2191 浏览 2 评论 0原文

我想问一下如何在 WSDL 文件的基础上生成 XML 格式的 SOAP 请求/响应。目标平台是 JVM,因此可以使用多种语言(例如 Java、Scala、Groovy、JRuby、Jython 等)。 SOAP 请求/响应生成应该纯粹在 XML 级别上完成,而不需要任何类生成和类加载(WSDL2Java、JAXB 或类似方法在这种情况下不合适)。应使用开源组件以编程方式完成生成。生成技术应该支持文档文字、rpc 编码和 rpc 文字风格,因此参数的正确编码应该由生成器处理。请求/响应消息应完全填充 ->即使对于空/空白值也应该生成空节点。

长话短说->我想以编程方式完成 SoapUI IDE 中可以完成的事情。我已经看过不同的 Java 相关库/框架(SAAJ、WSDL4J)或 Ruby (Savon),但我正在努力进一步推动它。

我正在处理的示例 Web 服务定义(WSDL 和 XSD)是 stockquote-ws.wsdlstockquote-schema.xsd

我想做的是:

SoapMessageGenerator generator = new SoapMessageGenerator("stockquote-ws.wsdl");
String request = generator.generateSoapRequest();
String response = generator.generateSoapResponse();

在这种情况下,请求应如下所示:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://centeractive.com/stockquote.wsdl" xmlns:stoc1="http://centeractive.com/stockquote.xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <stoc:GetLastTradePrice soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <stoc1:TradePriceRequest>
            <tickerSymbol xsi:type="xsd:string">?</tickerSymbol>
         </stoc1:TradePriceRequest>
      </stoc:GetLastTradePrice>
   </soapenv:Body>
</soapenv:Envelope>

...而响应应如下所示:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://centeractive.com/stockquote.wsdl" xmlns:stoc1="http://centeractive.com/stockquote.xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <stoc:GetLastTradePriceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <stoc1:TradePrice>
            <price xsi:type="xsd:float">?</price>
         </stoc1:TradePrice>
      </stoc:GetLastTradePriceResponse>
   </soapenv:Body>
</soapenv:Envelope>

I would like to ask you how I can generate a SOAP request/response in a XML format on the basis of the WSDL file. The target platform is JVM so a wide set of languages can be used (e.g. Java, Scala, Groovy, JRuby, Jython, etc.). The SOAP request/response generation should be done purely on the XML level without any class-generation and class-loading (WSDL2Java, JAXB or similar approaches are inappropriate in this case). Generation should be done programmatically with the usage of open-source components. The generation technique should support document-literal, rpc-encoded and rpc-literal flavors, so proper encoding of parameters should be handled by the generator. Request/response messages should be fully-populated -> empty nodes should be generated even for empty/blank values.

Cutting the long story short -> I would like to do programmatically the thing that is doable in SoapUI IDE. I already had a look at different Java-related libraries/frameworks (SAAJ, WSDL4J) or Ruby (Savon) but I am struggling to move it any further.

A sample Web-Service definition (WSDL and XSD) that I am working on is
stockquote-ws.wsdl and stockquote-schema.xsd.

What I would like to do is:

SoapMessageGenerator generator = new SoapMessageGenerator("stockquote-ws.wsdl");
String request = generator.generateSoapRequest();
String response = generator.generateSoapResponse();

In this case a request should look like this:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://centeractive.com/stockquote.wsdl" xmlns:stoc1="http://centeractive.com/stockquote.xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <stoc:GetLastTradePrice soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <stoc1:TradePriceRequest>
            <tickerSymbol xsi:type="xsd:string">?</tickerSymbol>
         </stoc1:TradePriceRequest>
      </stoc:GetLastTradePrice>
   </soapenv:Body>
</soapenv:Envelope>

... whereas a response should look like this:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://centeractive.com/stockquote.wsdl" xmlns:stoc1="http://centeractive.com/stockquote.xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <stoc:GetLastTradePriceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <stoc1:TradePrice>
            <price xsi:type="xsd:float">?</price>
         </stoc1:TradePrice>
      </stoc:GetLastTradePriceResponse>
   </soapenv:Body>
</soapenv:Envelope>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜空下最亮的亮点 2024-12-12 07:40:58

好的。我设法完全解决了这个问题。我从soapUI中提取了一些代码,并启动了一个开源项目,用Java以纯XML方式支持SOAP。类提取背后的主要原因是将负责生成 SOAP 消息的代码与与其他模块(例如,soapUI 图形用户界面等)紧密耦合的其余肥皂UI 代码分开。您可以找到项目在这里: https://github.com/reficio/soap-ws 不仅能够生成 SOAP 消息,而且还提供 SOAP 客户端和服务器。更多详细信息请参见:http://www.reficio.org/projects

OK. I managed to fully solve this problem. I have extracted some code from soapUI and started an open-source project to support SOAP in a purely XML way in Java. The main reason behind the class extraction was to separate the code that is responsible for the generation of the SOAP messages from the rest of the soapUIs code that is tightly coupled with other modules, such as soapUIs graphical user interface, etc. You can find the project here: https://github.com/reficio/soap-ws Not only is it able to generate SOAP messages, but also provides SOAP client and server. More details here: http://www.reficio.org/projects

爱的十字路口 2024-12-12 07:40:58

SOAPUI 库怎么样:

package com.bbog.soap;

import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlOperation;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter;
import com.eviware.soapui.model.iface.Operation;

public class WsdlAnalyzer {

    public static void main(String[] args) throws Exception {
        WsdlProject project = new WsdlProject();
        WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, "http://localhost:7000/Solicitud?wsdl");
        WsdlInterface wsdl = wsdls[0];
        for (Operation operation : wsdl.getOperationList()) {
            WsdlOperation op = (WsdlOperation) operation;
            System.out.println("OP:"+op.getName());
            System.out.println(op.createRequest(true));
            System.out.println("Response:");
            System.out.println(op.createResponse(true));
        }
    }
}

What about the SOAPUI library:

package com.bbog.soap;

import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlOperation;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter;
import com.eviware.soapui.model.iface.Operation;

public class WsdlAnalyzer {

    public static void main(String[] args) throws Exception {
        WsdlProject project = new WsdlProject();
        WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, "http://localhost:7000/Solicitud?wsdl");
        WsdlInterface wsdl = wsdls[0];
        for (Operation operation : wsdl.getOperationList()) {
            WsdlOperation op = (WsdlOperation) operation;
            System.out.println("OP:"+op.getName());
            System.out.println(op.createRequest(true));
            System.out.println("Response:");
            System.out.println(op.createResponse(true));
        }
    }
}
我恋#小黄人 2024-12-12 07:40:58

您可能对用于移动开发的kSOAP 项目感兴趣。 以下 kSOAP 教程将指导您如何序列化请求,以下部分将向您展示如何获取响应。

kSOAP 可以创建 SOAP 消息,而无需生成代理代码。这在移动开发中是必需的,因为其处理能力远低于桌面,并且代理类和库被认为比直接创建 SOAP 消息更繁重

You might be interested in kSOAP project which is used in mobile development. The following kSOAP tutorial will point you to how to serialize the request and the following section show you how to get the response.

kSOAP could create the SOAP message without having to generate the proxy code. This is needed in mobile development due to its processing power that is considerably less than the desktop and having proxy classes and library are considered heavier than directly creating the SOAP message

世俗缘 2024-12-12 07:40:58

IBM 文章

上面的文章似乎解决了我将尝试的技术对于您的情况:利用 XSLT 转换。毕竟您将从 XML 转向 XML。如果您比我运气好,找到(或者开发)从 WSDL 到伴随的 SOAP 请求所需的特定 XSLT 样式表,我很乐意了解它。

干杯,
维姆

IBM article

The above article seems to address the technique that I would try for your case: make use of XSLT transformation. After all you're going from XML to XML. If you have better luck than I finding (or developing of course) the specific XSLT stylesheet(s) that you need to go from a WSDL to accompanying SOAP request(s) I'd love to learn about it.

Cheers,
Wim

笑饮青盏花 2024-12-12 07:40:58

我实际上想做同样的事情。我一直在使用 javax.wsdl API 从 wsdl 中提取信息,并且尝试使用 javax.xml.soap API 来创建 SOAP 请求/响应。它们可能值得一看。

I am actually looking to do the same thing. I've been using the javax.wsdl API to pull information from the wsdl and I'm trying to use javax.xml.soap API to create the SOAP request/response. They might be worth taking a look at.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文