自己編寫webservice_HelloWorld
阿新 • • 發佈:2018-11-25
伺服器端
介面
注意:@webservice @webmethodpackage com.imooc;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* SEI
*
*/
@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name);
}
介面實現類
注意:@webservicepackage 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;
}
}
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);
}
}