1. 程式人生 > >使用axis2實現webService

使用axis2實現webService

        實現webService有多種實現方式,基於CXF和axis2都可以實現,但是該選擇哪種實現方式呢?

        1:如果應用程式需要多語言的支援,Axis2 應當是首選了

        2:如果應用程式是遵循 Spring 哲學路線的話,Apache CXF 是一種更好的選擇,特別對嵌入式的 Web Services 來說

        這裡我主要是使用axis2來實現的webService,因為需要對接的系統是採用的axis2作為服務端,我這邊就採用axis2建立一個客戶端來獲取資料。

      作為客戶端

public class WebserviceClient2 {
	private static long lastUpdateTime = 0;
	public static void main(String[] args) {
		try {
			//對外發布的webService的地址
            EndpointReference targetEPR = new EndpointReference("http://218.31.132.198:8092/zxris_rmt/services/configInfoService?wsdl");
            RPCServiceClient sender = new RPCServiceClient();
            Options options = sender.getOptions();
            //超時時間20s
            options.setTimeOutInMilliSeconds(2 * 20000L);
            options.setTo(targetEPR);
            /**
             * 引數:
             * 1:在網頁上執行 wsdl後xs:schema標籤的targetNamespace路徑
             * <xs:schema  targetNamespace="http://axis2.com">
             * 2:<xs:element name="test"> ======這個標籤中name的值
             */
            QName qname = new QName("http://api.WebService.business.ucms.app.rfid.zit.com", "getConfigInfo");
            QueryCondition queryCondition = new QueryCondition();
			queryCondition.setResultType(QueryCondition.ALL_CONFIG_INFO);
			queryCondition
					.setServiceId(QueryCondition.SERVICE_DETAIL_CONFIG_INFO);
			queryCondition.setUpdateTime(lastUpdateTime);

			String condition = JSONObject.fromObject(queryCondition).toString();
            //方法的入參
            Object[] param = new Object[]{condition};
            //這是針對返值型別的
            Class<?>[] types = new Class[]{String.class};
            /**
             * RPCServiceClient類的invokeBlocking方法呼叫了WebService中的方法。
             * invokeBlocking方法有三個引數
             * 第一個引數的型別是QName物件,表示要呼叫的方法名;
             * 第二個引數表示要呼叫的WebService方法的引數值,引數型別為Object[];
             * 第三個引數表示WebService方法的返回值型別的Class物件,引數型別為Class[]。
             * 當方法沒有引數時,invokeBlocking方法的第二個引數值不能是null,而要使用new Object[]{}。
             */
            Object[] response = sender.invokeBlocking(qname, param, types);
            System.out.println(response[0]);
        } catch (AxisFault e) {
            e.printStackTrace();
        }

	}
}

     如果作為客戶端來呼叫遠端服務的程式碼,上面的配置完全可以滿足需求

    作為服務端

我懶的寫了,以後再說吧!