----------------------------------------------------服务端--------------------------------------------------------------
1.创建一个java项目
2.创建一个类,加入Webservice注解(jdk1.6以上版本)
3.提供一个方法Hello
4.在main方法中调用jdk提供的发布服务的方法
@WebService
public class HelloService {
public String Hello(String name){
System.out.println("服务端被调用了。。。。");
return "hello" + name;
}
public static void main(String[] args) {
String address = "http://192.168.xx.xx:8080/hello";
Object implementor = new HelloService();
Endpoint.publish(address, implementor);
}
}
5.访问服务的wsdl文档(服务的发布地址+?wsdl)http://192.168.xx.xx:8080/hello?wsdl
能够看到xml文档就说明服务发布成功了。
--------------------------------------------------客户端---------------------------------------------
1.首先解析wsdl文件,生成客户端本地代码。
在命令行中使用jdk自带的wsimport命令解析wsdl文件(wsimport -s . http://192.168.xx.xx:8080/hello?wsdl)
2.将生成的java文件拷贝到客户端的项目中去。
3.通过本地代码创建一个代理对象。
4.通过代理对象实现远程调用。
public class Client {
public static void main(String[] args) {
HelloServiceService ss = new HelloServiceService();
//创建客户端代理对象
HelloService proxy = ss.getHelloServicePort();
String res = proxy.sayHello("小明");
System.out.println(res);
}
}
5.运行可以在控制台看到结果。