一、准备工作(以下为本实例使用工具)
1、MyEclipse10.7.1
2、JDK 1.6.0_22
二、创建服务端
1、创建【Web Service Project】,命名为【TheService】。
2、创建【Class】类,命名为【ServiceHello】,位于【com.hyan.service】包下。
3、编写供客户端调用的方法,即编译方法代码。
package com.hyan.service;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class ServiceHello {
public String getValue(String name){
return "我叫:"+name;
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:9001/Service/ServiceHello",new ServiceHello());
System.out.println("service success!");
}
}
4、进行编译
右击->Run As ->Java Application
5、测试结果
在网页中输入该网址:http://localhost:9001/Service/ServiceHello?wsdl
三、生成客户端
1、创建【Web Service Project】,命名为【TheClient】。
2、命令提示窗口执行生成命令。
win+R,输入cmd,敲击回车键
格式:wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址”
示例:
wsimport -s G:\\workspace\\webService\\TheClient\\src -p com.hyan.client -keephttp://localhost:9001/Service/ServiceHello?wsdl
3、刷新项目,检查生成类
右击包名,刷新,看到新生成的Java类
四、最终测试
1、创建【Class】类,命名为【ServiceTest】,位于【com.hyan.test】包下。
package com.hyan.test;
import com.hyan.client.ServiceHello;
import com.hyan.client.ServiceHelloService;
public class ServiceTest {
public static void main(String[] args) {
ServiceHello hello = new ServiceHelloService().getServiceHelloPort();
String name = hello.getValue("wangfei");
System.out.println(name);
}
}
2、编写测试方法,调用服务端方法,并打印返回的结果。