使用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. 查看生成的文件
如图所示,输出的文件保存在按指定的包名称构成的路径中(jaxws\client\stub)。

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方法调用服务接口。
使用wsimport和JAX-WS调用Web Service接口的更多相关文章
- ASP.NET不通过添加web引用的方式调用web service接口
尊重原著作:本文转载自http://bbs.csdn.net/topics/360223969 创建方法 //动态调用web服务 public static object InvokeWebSer(s ...
- php调用web service接口(.net开发的接口)
实例代码1: try { $this->soapClientObj = new SoapClient(self::URL . '?wsdl', array('connection_timeout ...
- PHP 调用web service接口(.net开发的接口)
实例代码1: try { $this->soapClientObj = new SoapClient(self::URL . '?wsdl', array('connection_timeout ...
- php5调用web service
工作中需要用php调用web service接口,对php不熟,上网搜搜,发现关于用php调用web service的文章也不多,不少还是php4里用nusoap这个模块调用的方法,其实php5里已经 ...
- php5调用web service (笔者测试成功)
转自:http://www.cnblogs.com/smallmuda/archive/2010/10/12/1848700.html 感谢作者分享 工作中需要用php调用web service接口, ...
- ORACLE存储过程调用Web Service
1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...
- delphi调用web service出现 Unable to retrieve the URL endpoint for Service/Port .....
delphi调用web service出现 Unable to retrieve the URL endpoint for Service/Port, 错误截图如下 查了很长时间, 发现在DataM ...
- C#使用SOAP调用Web Service
程序代码 using System;using System.IO;using System.Net;using System.Text; namespace ConsoleApplication1{ ...
- [Teamcenter 2007 开发实战] 调用web service
前言 在TC的服务端开发中, 能够使用gsoap 来调用web service. 怎样使用 gsoap , 參考 gsoap 实现 C/C++ 调用web service 接下来介绍怎样在TC中进行 ...
随机推荐
- Linux-tar命令(2)
tar 用于可以为文件和目录创建档案.可以为某一特定文件打包(备份文件), 也可以在档案中改变文件,或者向档案中加入新的文件. 主选参数: 主选参数仅能存在一个!不可同时存在!因为不可能同时压缩与解压 ...
- 用GDI+画验证码
1.新建一个窗体应用程序,在上面拖一个pictureBox对象,为其添加单击事件 2.创建GDI对象.产生随机数画入图片中.画线条.最后将图片到pictureBox中,代码如下: private vo ...
- select设置disable后ie修改默认字体颜色暂时解决
找了很多资料,终于在科学上网后找到了一个方法,虽然暂时不知道原理,但是已经实现了功能就是好的 select[disabled='disabled']::-ms-value { color: #000; ...
- Python中os和shutil模块实用方法集…
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- 【集美大学1411_助教博客】团队作业9——测试与发布(Beta版本)
写在前面的话 已经看到了大家的发布成果,很欣喜,虽然有的团队的产品还是有一点问题,但大家也都发布成功了,这就是软件的魅力.但还是要说一些问题,大家录的视频不是没人讲解就是讲得太快,在我看来这都没有在卖 ...
- 团队作业10——Beta版本事后诸葛亮
事后诸葛亮分析 1.总结的提纲内容: a. 项目管理之事后诸葛亮会议. 一.设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件要解决的是教师需要 ...
- 201521123036 《Java程序设计》第8周学习总结
本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 HashMap自定义排序 List<Map.Entry<Stri ...
- 201521123033《Java程序设计》第2周学习总结
1. 本周学习总结 answer:(1)学会用码云存储代码,并下载代码. (2)学会在java中使用函数,使代码更精炼. 2. 书面作业 Q1.使用Eclipse关联jdk源代码,并查看String对 ...
- 201521123067《Java程序设计》第1周学习总结
1.本周学习总结 在本周的java学习中,我知道了java的发展历程,JDK和JRE以及JVM的区别与联系,并学习了如何安装Eclipse和搭建java的环境,编写出了第一个java程序,明白了jav ...
- [转载]sqlserver、Mysql、Oracle三种数据库的优缺点总结
一.sqlserver优点:易用性.适合分布式组织的可伸缩性.用于决策支持的数据仓库功能.与许多其他服务器软件紧密关联的集成性.良好的性价比等:为数据管理与分析带来了灵活性,允许单位在快速变化的环境中 ...