使用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方法调用服务接口。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!