本文简单举例说明如何使用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接口的更多相关文章

  1. ASP.NET不通过添加web引用的方式调用web service接口

    尊重原著作:本文转载自http://bbs.csdn.net/topics/360223969 创建方法 //动态调用web服务 public static object InvokeWebSer(s ...

  2. php调用web service接口(.net开发的接口)

    实例代码1: try { $this->soapClientObj = new SoapClient(self::URL . '?wsdl', array('connection_timeout ...

  3. PHP 调用web service接口(.net开发的接口)

    实例代码1: try { $this->soapClientObj = new SoapClient(self::URL . '?wsdl', array('connection_timeout ...

  4. php5调用web service

    工作中需要用php调用web service接口,对php不熟,上网搜搜,发现关于用php调用web service的文章也不多,不少还是php4里用nusoap这个模块调用的方法,其实php5里已经 ...

  5. php5调用web service (笔者测试成功)

    转自:http://www.cnblogs.com/smallmuda/archive/2010/10/12/1848700.html 感谢作者分享 工作中需要用php调用web service接口, ...

  6. ORACLE存储过程调用Web Service

    1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...

  7. 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 ...

  8. C#使用SOAP调用Web Service

    程序代码 using System;using System.IO;using System.Net;using System.Text; namespace ConsoleApplication1{ ...

  9. [Teamcenter 2007 开发实战] 调用web service

    前言 在TC的服务端开发中, 能够使用gsoap 来调用web service. 怎样使用 gsoap  , 參考 gsoap 实现 C/C++ 调用web service 接下来介绍怎样在TC中进行 ...

随机推荐

  1. github+hexo搭建自己的博客网站(六)进阶配置(搜索引擎收录,优化你的url)

    详细的可以查看hexo博客的演示:https://saucxs.github.io/ 绑定了域名: http://www.chengxinsong.cn hexo+github博客网站源码(可以clo ...

  2. spring mvc:@RequestParam与@ModelAttribute异同

    关于spring mvc中的两个注解:@RequestParam.@ModelAttribute区别,原先并没有特别注意,直到最近找别人开发的一个小模块的bug时,才有意识的比较了两者的区别. 1.@ ...

  3. 低版本浏览器支持HTML5标签的方法

    最近刷了一道面试题,是关于低版本浏览器支持HTM5标签的写法,在网上找了一些,都行之有效,但是缺少整体总结,所以在这里总结一下,方便其他人过来阅读. IE低版本需要支持HTML5标签: 方法1.传统引 ...

  4. 详解HTTPS加速原理

    HTTPS是什么? http叫超文本传输协议,使用TCP端口80,默认情况下数据是明文传送的,数据可以通过抓包工具捕获到,因此在interner上,有些比较重要的站点的http服务器需要使用PKI(公 ...

  5. 移动端踩坑之旅-ios下fixed、软键盘相关问题总结

    最近一个项目掉进了移动端的大坑,包括ios下fixed布局,h5唤起键盘等问题,作为一个B端程序员,弱项就是浏览器的兼容性和移动端的适配(毕竟我们可以要求使用chrome),还好这次让我学习了一下相关 ...

  6. 10分钟搞懂Tensorflow 逻辑回归实现手写识别

    1. Tensorflow 逻辑回归实现手写识别 1.1. 逻辑回归原理 1.1.1. 逻辑回归 1.1.2. 损失函数 1.2. 实例:手写识别系统 1.1. 逻辑回归原理 1.1.1. 逻辑回归 ...

  7. 《Java从入门到放弃》JavaSE入门篇:异常

    异常!!!看看生活中的异常例子: 正常情况下,从家到公司上班,只需要20分钟!但如果在路上碰到堵车或修路或车突然自燃等问题,那就没办法正常去上班了.其中堵车或修路或车突然自燃等问题就属于异常. 碰到异 ...

  8. 小程序脚本语言WXS,你想要的都在这里了

    WXS脚本语言是 Weixin Script脚本的简称,是JS.JSON.WXML.WXSS之后又一大小程序内部文件类型.截至到目前小程序已经提供了5种文件类型. 解构小程序的几种方式,其中一种方式就 ...

  9. Centos7 & Docker & Jenkins & ASP.NET Core 2.0 自动化发布和部署

    写在前面 Docker一直很火热,一直想把原本的Jenkins自动部署工具搬到Docker上面,无奈今年一直忙于各种事情,迟迟未实施这个事情,正好迎来了dotnet core 2.0 的正式发布,升级 ...

  10. [转载]历上最强的音乐播放器(jetAudio-8.0.5.320-Plus-VX

    原文地址:历上最强的音乐播放器(jetAudio-8.0.5.320-Plus-VX-完全汉化版)下载作者:盖世天星 历上最强的音乐播放器(jetAudio-8.0.5.320-Plus-VX-完全汉 ...