接口
@WebService
public interface MyWebService {
int add(int a, int b);
}
实现类
@WebService(endpointInterface="com.sg.service.MyWebService",serviceName="MyService")
public class MyWebserviceImpl implements MyWebService {
@Override
public int add(int a, int b) {
System.out.println(a+"+"+b+"="+(a+b));
return a+b;
}
发布服务:
System.out.println("service start................");
MyWebserviceImpl myWebserviceImpl = new MyWebserviceImpl();
String address = "http://localhost:8089/myService";
Endpoint.publish(address, myWebserviceImpl);
System.out.println("service end ..............");
1,先写好服务器端 接口和实现
接口必须标注:@WebService
实现必须标注:@WebService
(endpointInterface="com.sg.service.MyWebService",serviceName="MyService")
2在服务器段写好main函数
MyWebserviceImpl myWebserviceImpl = new MyWebserviceImpl();
String address = "http://localhost:8089/myService";
Endpoint.publish(address, myWebserviceImpl);
3配置环境变量:
D:\webservice\apache-cxf-2.4.1\bin
4客户端工程src目录
cd:D:\eclipse3.6\eclipse\workspace\test_jdk1.6_WebService_client\src
D:\eclipse3.6
\eclipse\workspace\test_jdk1.6_WebService_client\src>D:\eclipse3.6
\eclipse\workspace\test_jdk1.6_WebService_client\src
wsdl2java -fe jaxws21 http://localhost:8089/myService?wsdl
jaxws21代表jdk的版本21代表jdk6 22的代表jdk7
如果不写jaxes21就默认的会是jdk7
5刷新一下工程会出现几个java类
客户端测试:
public static void main(String[] args) {
//对应服务器端实现类
//@WebService
(endpointInterface="com.sg.service.MyWebService",serviceName="MyService")
MyService myService = new MyService();
//获取一个接口:服务器端的代理接口
MyWebService myWebService =
myService.getMyWebserviceImplPort();
int add = myWebService.add(1, 1);
System.out.println(add);
}