【问题标题】:Dynamic Client WSDL动态客户端 WSDL
【发布时间】:2012-10-22 06:41:17
【问题描述】:

我是网络服务的新手。我的要求是一个 WSDL 链接

  1. 我要动态查找所有支持的操作(方法 由 WSDL 发布)
  2. 迭代列出的不同操作
  3. 动态获取每个操作的参数列表和返回值
  4. 获取Operation Name、Param和return的详细信息后动态消费一个Service

我能够编写一个动态客户端应用程序(Axis 2,WSDL4J),它将使用 WSDL 并给出方法名称、为方法定义的参数、数据类型等。 我创建了一个应用程序,它采用 WSDL 并为我提供 WSDL 中存在的所有方法。

Definition def = reader.readWSDL(null, wsdlURI);
Map services = def.getServices();
// Services when Iterated will give all the Method Names 
// .......
Operation operation = boperation.getOperation();
String OperationName = operation.getName();
System.out.println("OperationName :" + OperationName.toString());

除此之外,我不确定如何动态获取给定方法的参数名称。 非常感谢任何示例代码/教程

附上客户端的完整代码。

    public class DynamicInvoke {

    public static void main(String[] args) throws WSDLException {

        String wsdlURI = "WORKING_WSDL_LINK";
        DynamicInvoke di = new DynamicInvoke();
        List wsdlList = new ArrayList();

        wsdlList = di.buildComponents(wsdlURI);

    }

    public List buildComponents(String wsdlURI) throws WSDLException {
        // The list of components that will be returned
        List serviceList = Collections.synchronizedList(new ArrayList());

        // Create the WSDL Reader object
        WSDLFactory factory = WSDLFactory.newInstance();
        WSDLReader reader = factory.newWSDLReader();

        try {
            // Read the WSDL and get the top-level Definition object
            Definition def = reader.readWSDL(null, wsdlURI);

            java.util.Map<QName, Service> services = null;
            // Get the services defined in the document
            try {
                services = def.getServices();
            } catch (Exception e) {
                System.out.println("Cast Exception " + e.getMessage());
            }

            if (services != null) {
                // Create a component for each service defined
                Iterator serviceIter = (services).values().iterator();

                for (int i = 0; serviceIter.hasNext(); i++) {
                    // Create a new ServiceInfo component for each service found
                    ServiceInfo serviceInfo = new ServiceInfo();

                    // Populate the new component from the WSDL Definition read
                    populateComponent(serviceInfo, (Service) serviceIter.next());

                    // Add the new component to the List to be returned
                    serviceList.add(serviceInfo);
                }
            }
        }

        catch (Throwable t) {
            // Process the error/exception
            System.err.println(t.getMessage());
        }

        // return the List of services we created
        return serviceList;
    }


    private ServiceInfo populateComponent(ServiceInfo component, Service service)
            throws WSDLException {
        // Get the qualified service name information
        QName qName = service.getQName();

        // Get the service's namespace URI
        String namespace = qName.getNamespaceURI();

        // Use the local part of the qualified name for the component's name
        String name = qName.getLocalPart();

        // Get the defined ports for this service
        Map ports = service.getPorts();

        // Use the Ports to create methods for all request/response messages
        // defined
        Iterator portIter = ports.values().iterator();



        while (portIter.hasNext()) {
            // Get the next defined port
            Port port = (Port) portIter.next();

            // Get the Port's Binding
            javax.wsdl.Binding binding = port.getBinding();

            // Now we will create operations from the Binding information
            List operations = buildOperations(binding);

            // Process methods built from the binding information
            Iterator operIter = operations.iterator();

            while (operIter.hasNext()) {
                BindingOperation boperation = (BindingOperation) operIter.next();
                Operation operation = boperation.getOperation();

                // Get all the QName,Port Name,Name Space, WebService Name...
                System.out.println("Port Name =" + port.getName());
                System.out.println("QName = " + qName.toString());
                System.out.println("Namespace = " + namespace.toString());
                System.out.println("WebService Name = " + name.toString());

                // Get All the Method Name...
                String OperationName = operation.getName();
                System.out.println("OperationName :" + OperationName.toString());

                Input inDef = operation.getInput();
                String ParamName = inDef.getName();
                System.out.println("inDef--->" + ParamName);
                Message inMessage = inDef.getMessage();
                Map parts = inMessage.getParts();


                System.out.print("\nAxis parameters gathered:\nTargetNamespace = " +"\n"+
                        "Service Name = "+namespace.toString() +"\n"+
                        "Port Name = "+port.getName() +"\n"+
                        "Operation Name = "+operation.getName()+"\n"+
                        "Input Parameters = ");



            }
        }

        return component;
    }

    private List buildOperations(Binding binding) {
        // TODO Auto-generated method stub

        List list = binding.getBindingOperations();

        System.out.println("Bindings :" + list);
        return list;

    }

}

【问题讨论】:

    标签: wsdl uddi


    【解决方案1】:

    您需要使用 wsdl2java-tool(它与 axis2-zip 打包在一起)从给定的 WSDL 编译存根代码。 然后你会发现你的 web 服务相关的类,包括所有的方法,为你准备好参数。

    例如,见Step 5 here

    【讨论】:

      猜你喜欢
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 2010-12-01
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多