服务端
package com.webservice.HelloWorld;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* SEI
* webservice接口类,声明方法sayHello
* @author ZhangJintao
*/
@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name) ;
}
package com.webservice.HelloWorld;
import javax.jws.WebService;
/**
* webservice接口实现类,实现sayHello方法
* @author ZhangJintao
*/
@WebService
public class HelloWSImpl implements HelloWS{
@Override
public String sayHello(String name) {
System.out.println("server say Hello . name = " + name);
return "Hello"+name;
}
}
package com.webservice.HelloWorld;
import javax.xml.ws.Endpoint;
/**
* 发布webservice
* @author ZhangJintao
*/
public class ServerTest {
public static void main(String[] args) {
//指定webservice的访问url
String address = "http://192.168.0.101/test/hello" ;
Endpoint.publish(address, new HelloWSImpl());
System.out.println("发布成功!!!");
}
}
客户端
1、打开cmd,进入客户端代码src目录下
在cmd下执行命令【wsimport -keep http://192.168.0.101/test/hello?wsdl】,wsimport 是jdk自带工具,会在src目录下自动生成客户端代码。
2、创建客户端代码
package client;
import com.webservice.helloworld.HelloWSImpl;
import com.webservice.helloworld.HelloWSImplService;
public class ClientTest {
public static void main(String[] args) {
HelloWSImplService factory = new HelloWSImplService();
HelloWSImpl helloWS = factory.getHelloWSImplPort();
helloWS.sayHello("aaaa撒啊");
}
}