1.使用Eclipse建立Web Service服务端和客户端
(1)服务器端
首先,在eclipse中像建立一个web项目一样,new->Dynamic Web Project,项目名为helloService
接着,建一个需要暴露给外部的方法,
package service;
public class HelloService {
public String say(String name) throws InterruptedException{
return "hello "+name;
}
}
然后呢,右击这个项目,new -> other->web services->webservice,选择需要暴露的实现,service.HelloService,然后选择发布
通过tomcat发布的,直接start即可。常用的框架有,cxf、 axis、 axis2等,这里选择了axis,发布之后,我们打开网页输入地址即可打开它的wsdl:http://localhost:8280/helloService/services/HelloService?wsdl。
(2)客户端
需要使用client去连这个Webservice服务了,新建一个java工程(都可以),然后新建一个Webservice client就可以,输入wsdl地址,finish即可,然后可以看到目录下的Webservice java类,新建一个test,去测试
package test;
import java.rmi.RemoteException;
import service.HelloService;
import service.HelloServiceProxy;
public class Test {
public static void main(String[] args) throws RemoteException {
HelloServiceProxy helloPxy = new HelloServiceProxy();
HelloService service = helloPxy.getHelloService();
String res = service.say("yyf");
System.out.println(res);
}
}