一、webservice作用:
各個應用程序之間的交流就需要WebService來作爲相互交流的中間人。
二、webservice簡單示例
1、 先建一個項目,建一個@WebService的服務類
package webservice;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;
/**
* 1.這爲其中一個項目,此類在webservice包下
* 2.JDK版本號爲1.6
* 3.如果控制檯輸出發佈成功,證明發布成功
*/
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class ExplodMethod {
/*
* 被其他程序調用的方法
*/
public String publicMsg(String words){
String res="";
for (char ch : words.toCharArray()) {
res+="/t"+ch+"/t";
}
return res;
}
/*
* 測試發佈方法
*/
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:8080/Service/ExplodMethod", new ExplodMethod());
System.out.println("發佈成功!");
}
}
其中可能出現的問題如下:
如果瀏覽器出現這個,證明發布成功:
2.另建一個項目,通過地址將@WebService服務類信息傳遞過來
cmd 命令如下:wsimport -s D:\workspaces\thinkInJava\src\ -p webservice.service -keep http://127.0.0.1:8080/Service/ExplodMethod?wsdl
意思爲:將剛發佈的項目的@WebService信息加到thinkInjava\src下
生成的文件爲:
可能出現的錯誤:
3.測試類
package webservice.service;
public class WebserviceTest {
public static void main(String[] args) {
ExplodMethod explodMethod=new ExplodMethodService().getExplodMethodPort();
String str=explodMethod.publicMsg("this is Test Message");
System.out.println("str:"+str);
}
}