zoukankan      html  css  js  c++  java
  • 使用wsimport和JAX-WS调用Web Service接口

    本文简单举例说明如何使用wsimport工具和JAX-WS API调用Web Service接口。
    此方法的优点:使用JDK自带的工具和API接口,无需依赖第三方库。


    JDK版本:1.8.0_141
    开发工具:Eclipse

    服务端源代码:下载
    客户端源代码:下载


    1. 使用JDK自带的wsimport工具根据WSDL生成web service client stub
    1.1. 确保已安装JDK1.6版本或更高版本
    1.2. 确保WebService服务已经启动
    1.3. 在命令行运行如下命令:

     
    参数说明:
    -d  指定生成输出文件的保存路径(.class文件,根据需要决定是否生成class文件)
    -s  指定生成的java源文件的保存路径(.java文件,根据需要决定是否生成java源文件)
    -p  指定生成的java类的包(package)名称
    http://localhost:8888/HelloService表示WebService URL地址,URL地址后面必须添加“?WSDL”参数。WSDL参数也可以是小写(wsdl)。
    要查看服务具体提供了哪些操作,请在URL后面添加“?Tester”参数。(有时候传递Tester并不一定能返回结果。这和服务的发布方式有关)
    1.4. 查看生成的文件
    如图所示,输出的文件保存在按指定的包名称构成的路径中(jaxwsclientstub)。
     
    2. 把生成的client stub类导入到eclipse工程中。
     

    3. 编写客户端代码
    创建jaxws.client. HelloAppClient类。
    客户端代码入下所示:

    package jaxws.client;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.xml.namespace.QName;
    import javax.xml.ws.Service;
    
    import jaxws.client.stub.HelloService;
    
    public class HelloAppClient {
        public static void main(String[] args) {
            sayHello();
        }
    
        public static void sayHello() {
            URL url = null;
            try {
                url = new URL("http://localhost:8888/HelloService");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            Service s = Service.create(url, new QName("http://service.jaxws/", "HelloServiceService"));
            HelloService hs = s.getPort(new QName("http://service.jaxws/", "HelloServicePort"), HelloService.class);
            String helloMessage = hs.sayHello("Tom");
            System.out.println(helloMessage);
            
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    注意:WebService中所说的Port和网络端口(port)不是一个概念。
    以下WSDL文档中的一些简单的概念解释(如果想深入了解请查看WSDL标准):

    A WSDL document defines services as collections of network endpoints, or ports. In WSDL, the abstract definition of endpoints and messages is separated from their concrete network deployment or data format bindings. This allows the reuse of abstract definitions: messages, which are abstract descriptions of the data being exchanged, and port types which are abstract collections of operations. The concrete protocol and data format specifications for a particular port type constitutes a reusable binding. A port is defined by associating a network address with a reusable binding, and a collection of ports define a service. Hence, a WSDL document uses the following elements in the definition of network services:

    • Types– a container for data type definitions using some type system (such as XSD).
    • Message– an abstract, typed definition of the data being communicated.
    • Operation– an abstract description of an action supported by the service.
    • Port Type–an abstract set of operations supported by one or more endpoints.
    • Binding– a concrete protocol and data format specification for a particular port type.
    • Port– a single endpoint defined as a combination of a binding and a network address.
    • Service– a collection of related endpoints.

    代码说明:
    (1)url表示WebService服务地址。此地址后面可以添加也可以不添加“?WSDL”参数。
    (2)Service提供WebService服务的客户端视图(client view)。它作为endpoint的客户端代理,用于分发(dispatch)对远程Service操作(Operation)的调用(通俗的说就是把某个Service上的方法调用发送给远程服务器上提供服务的那个Service)。
    (3)HelloService表示WebService服务client stub(类似于WebService服务在客户端的一个镜像,供客户端代码调用其接口)。
    (4)通过hs.sayHello方法调用服务接口。

  • 相关阅读:
    基本语法-函数
    基本语法-变量
    python+selenium2自动化------quit()和close()区别
    docker搭建bwapp漏洞测试环境
    软件测试---登录功能的测试用例
    Faker开源库构造测试数据
    selenium自动化简易测试框架总结
    ddt+unittest+HTMLTestRunner,生成测试报告时,报告中没有输出测试用例函数的描述信息
    python+selenium2自动化---PageObject模式+unittest结合实现自动化
    python+selenium2自动化---关键字驱动+unittest结合实现自动化
  • 原文地址:https://www.cnblogs.com/yitouniu/p/7640079.html
Copyright © 2011-2022 走看看