在上一篇WebService實例中,基於jdk1.6以上的javax.jws 發布webservice接口。這篇博文則主要用eclipse/myeclipse 使用axis插件進行發布和調用WebService。
1. 下載axis,並解壓到tomcat/webapps目錄下
官網地址:http://axis.apache.org/axis2/java/core/download.cgi
下載 axis2-xx-war.zip,並解壓到tomcat/webapps
2. 在tomcat部署axis2
啟動tomcat, 可以看到多了個axis2文件
在瀏覽器輸入:http://localhost:8080/axis2/
看到axis界面,則成功發布
3. 在eclipse/myeclipse 安裝axis插件
將下載下來的axis2-eclipse-codegen-plugin-x.x.x.zip和axis2-eclipse-service-plugin-x.x.x.zip 解壓,解壓之后的jar文件復制到eclipse/myeclipse 的dropins目錄下,重啟eclipse/myeclipse,右鍵File->New->Other 可以看到axis插件已經安裝成功。
4. 發布WebService
將下載下來的axis2-x.x.x-bin 解壓,將其中的lib架包添加置項目中。
新建class類,用於發布。
編譯該類之后,用axis2發布該類。
右鍵New -> File -> Other -> Axis2 wizards -> Axis2 Services Archiver 。
選擇該class類生成的路徑,注意只到classes目錄下, 然后next,勾上Skip WSDL,點擊next,點擊next,service填寫發布的名稱, class name填寫路徑,包名加上類名,然后選擇發布的方法。繼續next,選擇tomcat/webapps目錄下的axis/web-inf/service。
發布成功后,啟動tomcat,在瀏覽器輸入:http://localhost:8080/axis2/services/listServices 。可以看到要發布的webservice ,點擊該項目,進入wsdl界面。
/** * * Title: AxisServiceHello * Description: Axis2 發布 * Version:1.0.0 * @author panchengming */
public class AxisServiceHello {
/** 供客戶端調用方法 * @param name 傳入參數 * @return String 返回結果 * */
public String getValue(String name){
return "Axis 歡迎你! "+name;
}
}
5. 調用WebService
新建一個class類,用於調用發布的webservice。
可以使用rpc或document兩種方法調用,運行main方法,看到打印消息,調用成功。
注:調用需要將tomcat服務啟動,在瀏覽器輸入wsdl地址能夠查看。
import java.io.IOException;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
/** * * Title: AxisClientHello * Description: webService 客戶端調用 * Version:1.0.0 * @author panchengming */
public class AxisClientHello {
private final static String url="http://192.168.1.105:8080/axis2/services/AxisServiceHello?wsdl"; //wsdl地址
private final static String data="PanChengMing"; //參數
private final static String tns = "http://service.pcm.com"; //命名空間
private final static String method="getValue"; //調用的方法
//調用webservice
public static void main(String[] args) throws IOException{
getRPC(); //調用方法一
getDocument(); //調用方法二
}
/** * 方法一: * 應用rpc的方式調用 這種方式就等於遠程調用, * 即通過url定位告訴遠程服務器,告知方法名稱,參數等, 調用遠程服務,得到結果。 * 使用 org.apache.axis2.rpc.client.RPCServiceClient類調用WebService * 【注】: 如果被調用的WebService方法有返回值 應使用 invokeBlocking 方法 該方法有三個參數 第一個參數的類型是QName對象,表示要調用的方法名; 第二個參數表示要調用的WebService方法的參數值,參數類型為Object[]; 當方法沒有參數時,invokeBlocking方法的第二個參數值不能是null,而要使用new Object[]{}。 第三個參數表示WebService方法的 返回值類型的Class對象,參數類型為Class[]。 如果被調用的WebService方法沒有返回值 應使用 invokeRobust 方法 該方法只有兩個參數,它們的含義與invokeBlocking方法的前兩個參數的含義相同。 在創建QName對象時,QName類的構造方法的第一個參數表示WSDL文件的命名空間名, 也就是 <wsdl:definitions>元素的targetNamespace屬性值。 * */
@SuppressWarnings("rawtypes")
public static void getRPC() throws AxisFault{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定調用WebService的URL
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
// 指定要調用的WSDL文件的命名空間及getValue方法
QName qn = new QName(tns, method);
// 指定getValue方法的參數值
Object[] ob = new Object[] { data };
// 指定getValue方法返回值的數據類型的Class對象
Class[] classes = new Class[] { String.class };
// 調用getValue方法並輸出該方法的返回值
System.out.println(serviceClient.invokeBlocking(qn, ob, classes)[0]);
}
/** * 方法二: 應用document方式調用 * 用ducument方式應用現對繁瑣而靈活。現在用的比較多。因為真正擺脫了我們不想要的耦合 */
public static void getDocument() throws AxisFault{
OMElement result = null;
try {
Options options = new Options();
// 指定調用WebService的URL
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMFactory fac = OMAbstractFactory.getOMFactory();
// 命名空間
OMNamespace omNs = fac.createOMNamespace(tns, "");
OMElement ot = fac.createOMElement(method, omNs);
OMElement symbol = fac.createOMElement("name", omNs);
symbol.addChild(fac.createOMText(symbol, data));
ot.addChild(symbol);
result=sender.sendReceive(ot);
System.out.println(result);
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}
結語:使用axis實現webservice 暫時告一段落了,這次的demo和上篇的webservice的demo 我整合成了一個項目,發布到我的github上了 ,https://github.com/xuwujing/webservice_project 。 有興趣的可以看看。