本实例所用工具:
1、eclipse-luna
2、jdk1.6.0_45
一、创建服务端
1.新建 Java Project,命名为TheService
2.在TestService下创建包,包名为com.xx.service,在该包下新建类,命名为ServiceHello
package com.xx.service; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService public class ServiceHello { public String getValue(String name){ return "Hello:"+name; } public static void main(String[] args) {
Endpoint.publish("http://localhost:9091/Service/ServiceHello", new ServiceHello()); System.out.println("Service success!"); } }
getValue(String name) 为服务端供客户端调用的方法,main方法第一句的作用是发布服务端,端口号任意,但必须未被调用。
3.编译该类,若编译失败,请更新jdk版本
编译成功
4.测试发布结果
测试地址:
http://localhost:9091/Service/ServiceHello?wsdl(Service为固定的,ServiceHello为类名,?wsdl为固定的页面)
输入地址,看到xml内容,说明发布成功
二、创建客户端
1.新建 Java Project,命名为TheClient
2.在TestClient下创建包,包名为com.xx.client
3.命令提示窗口生成客户端
格式:wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址”
示例:
wsimport -s D:\Luna\workspaces\travelsky\TheClient\src -p com.xx.client -keep http://localhost:9001/Service/ServiceHello?wsdl
说明:
1)"src目录"地址不可含空格
2)“wsdl发布地址”不要漏了“?wsdl”
4.刷新TestClient,检查生成类
三、测试
在com.xx.client包下,新建类ServiceTest
package com.xx.test; import com.xx.client.ServiceHello; import com.xx.client.ServiceHelloService; public class ServiceTset { public static void main(String[] args) { ServiceHello hello=new ServiceHelloService().getServiceHelloPort(); String name=hello.getValue("daihx"); System.out.println(name); } }
编译测试方法,返回结果
这应该只是最基础的入门实例,还需要结合具体项目加深对webservice的理解。