服务器端
接口
注意:@webservice @webmethod
package com.imooc; import javax.jws.WebMethod; import javax.jws.WebService; /** * SEI * */ @WebService public interface HelloWS { @WebMethod public String sayHello(String name); } @WebService public interface HelloWS { @WebMethod public String sayHello(String name); }
接口实现类
注意:@webservice
package com.imooc; import javax.jws.WebService; @WebService public class HelloWSImpl implements HelloWS { @Override public String sayHello(String name) { System.out.println("server sayhello ..."+name);//在服务器端打印字符串 return "hello "+name; } } @WebService public class HelloWSImpl implements HelloWS { @Override public String sayHello(String name) { System.out.println("server sayhello ..."+name);//在服务器端打印字符串 return "hello "+name; } }
service发布方法
package com.imooc; import javax.xml.ws.Endpoint; /* * 发布webservice */ public class Start { public static void main(String[] args) { String address="http://localhost:8989/hello"; Endpoint.publish(address, new HelloWSImpl()); System.out.println("发布webservice成功。。。。"); } }
发布成功后
http://localhost:8989/hello?wsdl
客户端
在命令行窗口上进入上面的src的目录下
输入命令 wsimport -keep http://localhost:8989/hello?wsdl
(wsimport后面有空格 -keep后面有空格)
刷新项目后
在客户端调用webservice
package com.imooc; public class Main { public static void main(String[] args) { HelloWSImplService factory=new HelloWSImplService(); HelloWSImpl helloWS=factory.getHelloWSImplPort(); String ret=helloWS.sayHello("123456"); System.out.println(ret); } }