webService服务端和客户端的创建

本文介绍了一个简单的Java WebService示例,包括服务端创建、客户端生成及最终测试过程。特别注意JDK版本、命令路径及生成命令的完整性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作。

 

一、准备工作(以下为本实例使用工具)

1、MyEclipse10.7.1

2、JDK 1.6.0_22

 

二、创建服务端

1、创建【Web Service Project】,命名为【TheService】。


id="iframe_0.6760989567264915" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7887%2F2c2b9a5b-8476-3cc6-ae28-91affd0ae861.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.6760989567264915" frameborder="0" scrolling="no" height="403" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.17554417555220425" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7889%2F9f5965f0-fe48-317e-8db9-68dcc19abd8c.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.17554417555220425" frameborder="0" scrolling="no" height="728" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

2、创建【Class】类,命名为【ServiceHello】,位于【com.hyan.service】包下。


id="iframe_0.7781127945054322" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7891%2F156170f0-f6a7-3abe-bff8-4ee88633418c.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.7781127945054322" frameborder="0" scrolling="no" height="807" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.39248022926039994" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7893%2Fd31a1ebd-1607-3193-811e-5d1b27ef251a.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.39248022926039994" frameborder="0" scrolling="no" height="755" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

3、编写供客户端调用的方法,即编译方法代码。


id="iframe_0.845266574062407" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7895%2F3324a266-78e2-3d27-a6ea-9e55e0e8403e.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.845266574062407" frameborder="0" scrolling="no" height="546" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

4、进行编译

说明:编译失败的话,请将该项目引用的jdk设置为1.6.0_17版本或更高版本


id="iframe_0.556740575702861" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7897%2F03ee0b15-0e84-326c-a4ff-de4e729bdc76.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.556740575702861" frameborder="0" scrolling="no" height="795" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.4027682705782354" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7899%2F5ea8baa7-3fca-3314-addc-ba0e9b101e9c.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.4027682705782354" frameborder="0" scrolling="no" height="189" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

5、测试结果

测试地址:http://localhost:9001/Service/ServiceHello?wsdl


id="iframe_0.7108296267688274" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7901%2F09b96287-a962-3fd7-bcf3-1c8ba19af4e7.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.7108296267688274" frameborder="0" scrolling="no" height="799" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

三、生成客户端

1、创建【Web Service Project】,命名为【TheClient】。


id="iframe_0.8426385105121881" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7903%2F23fe293c-5db1-3098-9ada-48c2c5f7f3f2.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.8426385105121881" frameborder="0" scrolling="no" height="825" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.6877955165691674" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7905%2F0fe3b620-284d-3431-b322-29cd2292f6e6.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.6877955165691674" frameborder="0" scrolling="no" height="725" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.2201041574589908" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7907%2F3b96eade-6324-33ad-85e9-9fba53866686.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.2201041574589908" frameborder="0" scrolling="no" height="463" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

2、命令提示窗口执行生成命令。

格式:wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址”

示例:

wsimport -s G:\\workspace\\webService\\TheClient\\src -p com.hyan.client -keep http://localhost:9001/Service/ServiceHello?wsdl

说明:

1)"src目录"地址不可含空格

2)“wsdl发布地址”不要漏了“?wsdl”


id="iframe_0.9470278723165393" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7909%2Fe01981dc-d50e-30d9-ba9f-e801d8a3361c.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.9470278723165393" frameborder="0" scrolling="no" height="500" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

3、刷新项目,检查生成类


id="iframe_0.9849415293429047" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7911%2F44f65c4e-7e1a-3d22-9e38-5cc19974b457.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.9849415293429047" frameborder="0" scrolling="no" height="677" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.06490700831636786" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7913%2F03d1b9e5-4ab0-3f3b-a9a8-1753ccb4d4b2.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.06490700831636786" frameborder="0" scrolling="no" height="524" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
 

 

四、最终测试

1、创建【Class】类,命名为【ServiceTest】,位于【com.hyan.test】包下。


id="iframe_0.11248390842229128" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7915%2F4a79b4a6-a3a3-3276-b47d-1dfe1e0839ff.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.11248390842229128" frameborder="0" scrolling="no" height="648" style="box-sizing: border-box; border-style: none; width: 1057px;">

id="iframe_0.2287283323239535" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7917%2F73f2fb65-22ce-3d9d-8da8-00b987bb0fb1.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.2287283323239535" frameborder="0" scrolling="no" height="759" style="box-sizing: border-box; border-style: none; width: 1057px;">
  
id="iframe_0.3220948278903961" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7919%2F657933ba-9fcd-3f36-b1e7-e521be538df7.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.3220948278903961" frameborder="0" scrolling="no" height="552" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

2、编写测试方法,调用服务端方法,并打印返回的结果。


id="iframe_0.4776493455283344" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7921%2F77e6b32d-d017-3127-9322-dde5a85b6966.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.4776493455283344" frameborder="0" scrolling="no" height="594" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.1128573368769139" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7923%2F1f61176d-25c9-346b-96f2-574c83e72b93.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.1128573368769139" frameborder="0" scrolling="no" height="171" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

五、注意事项

1、JDK版本过低问题

报类似如下的异常:runtime modeler error: Wrapper class com.hyan.service.jaxws.GetValue is not found. Have you run APT to generate them?


id="iframe_0.31439591897651553" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7928%2F389198e5-a59d-353e-8368-3c94a722ec67.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.31439591897651553" frameborder="0" scrolling="no" height="470" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

原因:JDK版本低于1.6.0_17

解决方法:调整该服务端项目所引用的JDK版本为安装的高版本JDK


id="iframe_0.9181266410741955" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7930%2F9787565a-69f7-3f27-b780-0ff7e4a3393d.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.9181266410741955" frameborder="0" scrolling="no" height="723" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.9188643912784755" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7932%2F44b795ab-1558-33a4-a7e8-1e8e1f8c911a.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.9188643912784755" frameborder="0" scrolling="no" height="596" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.47073714388534427" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7934%2F97e2942a-d21f-3ce2-b141-eedabe10d512.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.47073714388534427" frameborder="0" scrolling="no" height="655" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.9720514146611094" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7936%2F1ced02c9-945c-3ddb-9785-04c533c9f0b5.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.9720514146611094" frameborder="0" scrolling="no" height="620" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.3259557818528265" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7938%2F373d75b2-1c54-3bb7-91e9-35be5d77f98e.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.3259557818528265" frameborder="0" scrolling="no" height="554" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.22735052439384162" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7940%2F7e755db6-e732-3b14-bfbf-145812163f2f.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.22735052439384162" frameborder="0" scrolling="no" height="640" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.28746455046348274" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7942%2F216b9ca3-0621-33f9-b962-df6fd0875da8.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.28746455046348274" frameborder="0" scrolling="no" height="641" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.8259523403830826" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7944%2Fd9831e5d-4b6e-31d9-ae0f-48e1ff39e6a9.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.8259523403830826" frameborder="0" scrolling="no" height="619" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.5364624322392046" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7946%2F50eac14c-8b20-3c62-ac31-459e8b39997b.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.5364624322392046" frameborder="0" scrolling="no" height="651" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.840472050011158" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7948%2Fd5662634-0f70-3d25-831d-e00af5e5162a.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.840472050011158" frameborder="0" scrolling="no" height="593" style="box-sizing: border-box; border-style: none; width: 1057px;">
 
id="iframe_0.04620129195973277" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7950%2Fce7642b6-7c84-3f4f-8f55-36959d05b68c.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.04620129195973277" frameborder="0" scrolling="no" height="425" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

 

2、生成命令路径含空格问题

报类似如下的异常:directory not found: G:\SVN_FILE\GB\05


id="iframe_0.23414612980559468" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7952%2F455c809d-3cc9-352a-aaee-d381f4a3f5df.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.23414612980559468" frameborder="0" scrolling="no" height="718" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

原因:客户端src路径不可含有空格

解决方法:在不含空格路径的文件夹下重新创建客户端即可。

 

3、生成命令不完整问题

报类似如下的异常:[ERROR] failed.noservice=Could not find wsdl:service in the provided WSDL(s):


id="iframe_0.5649060576688498" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl2.iteye.com%2Fupload%2Fattachment%2F0101%2F7954%2Ffef0cef5-b44b-35bb-9f69-5fcdd23d835d.png&maxWidth=1057&origin=http://www.cnblogs.com&iframeId=iframe_0.5649060576688498" frameborder="0" scrolling="no" height="451" style="box-sizing: border-box; border-style: none; width: 1057px;">
 

原因:生成命令末尾缺少“?wsdl”

解决方法:补上即可。

错误不完整的命令示例:

wsimport -s G:\\workspace\\webService\\TheClient\\src -p com.hyan.client -keep http://localhost:9001/Service/ServiceHello

正确完整的命令示例:

wsimport -s G:\\workspace\\webService\\TheClient\\src -p com.hyan.client -keep http://localhost:9001/Service/ServiceHello?wsdl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值