简体   繁体   English

什么是用于动态SOAP客户端操作的优秀Java库?

[英]What's a good Java library for dynamic SOAP client operations?

I've been searching for SOAP client libraries for Java and have found a plethora of libraries based on the idea of building stub and proxy classes based on a WSDL. 我一直在寻找Java的SOAP客户端库,并且已经基于构建基于WSDL的存根和代理类的想法找到了大量的库。 I'm interested in allowing the user to enter a WSDL at runtime, parsing the WSDL, then allowing the user to perform operations on the Web Service. 我有兴趣允许用户在运行时输入WSDL,解析WSDL,然后允许用户在Web服务上执行操作。

Does anyone know of a good SOAP client library which will allow this runtime use? 有谁知道一个好的SOAP客户端库,它将允许这个运行时使用? Or is there a way I can use the axis2 wsdl2java functionality to build stubs into the classloader and use them at runtime? 或者有没有办法可以使用axis2 wsdl2java功能将存根构建到类加载器中并在运行时使用它们?

Later than never. 从来没有。 :) :)

You should achieve that in two steps: 你应该分两步完成:

  • 1) parse the WSDL informed by the user to retrieve the available operations. 1)解析用户通知的WSDL以检索可用的操作。 Refer to this question to know how to do this in a simple way. 请参阅此问题以了解如何以简单的方式执行此操作。

  • 2) Create a dynamic client to send a request using the selected operations. 2)创建动态客户端以使用所选操作发送请求。 It can be done by using the Dispatch API from Apache CXF . 可以使用Apache CXFDispatch API来完成。

Build the Dispatch object for the dynamic client (It can be created on the fly by informing web service endpoint, port name, etc): 为动态客户端构建Dispatch对象(可以通过通知Web服务端点,端口名称等来动态创建):

package com.mycompany.demo;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class Client {
  public static void main(String args[]) {
    QName serviceName = new QName("http://org.apache.cxf", "stockQuoteReporter");
    Service s = Service.create(serviceName);

    QName portName = new QName("http://org.apache.cxf", "stockQuoteReporterPort");
    Dispatch<DOMSource> dispatch = s.createDispatch(portName,
                                                  DOMSource.class,
                                                  Service.Mode.PAYLOAD);
    ...
  }
}

Construct the request message (In the example below we are using DOMSource ): 构造请求消息(在下面的示例中,我们使用DOMSource ):

// Creating a DOMSource Object for the request
DocumentBuilder db = DocumentBuilderFactory.newDocumentBuilder();
Document requestDoc = db.newDocument();
Element root = requestDoc.createElementNS("http://org.apache.cxf/stockExample", "getStockPrice");
root.setNodeValue("DOW");
DOMSource request = new DOMSource(requestDoc);

Invoke the web service 调用Web服务

// Dispatch disp created previously
DOMSource response = dispatch.invoke(request);

Recommendations: 建议:

  • Use ((BindingProvider)dispatch).getRequestContext().put("thread.local.request.context", "true"); 使用((BindingProvider)dispatch).getRequestContext().put("thread.local.request.context", "true"); if you want to make the Dispatch object thread safe. 如果你想让Dispatch对象线程安全。
  • Cache the Dispatch object for using later, if it is the case. 如果是这种情况,请稍后缓存Dispatch对象以供使用。 The process o building the object is not for free. 构建对象的过程不是免费的。

Other Methods 其他方法

There are other methods for creating dynamic clients, like using CXF dynamic-clients API. 还有其他方法可用于创建动态客户端,例如使用CXF 动态客户端 API。 You can read in project's index page: 您可以在项目的索引页面中阅读:

CXF supports several alternatives to allow an application to communicate with a service without the SEI and data classes CXF支持多种替代方案,允许应用程序在没有SEI和数据类的情况下与服务进行通信

I haven't tried that myself but should worth giving it a try. 我自己没试过,但值得尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM