Axis2框架實現WebService

Axis2快速入門:http://axis.apache.org/axis2/java/core/docs/quickstartguide.html

一、Eclipse axis2插件安裝

Axis2主頁上有關於插件的安裝方法;見http://axis.apache.org/axis2/java/core/tools/eclipse/wsdl2java-plugin.html#Installation

Axis2下載:http://axis.apache.org/axis2/java/core/download.cgi


這兩個壓縮包解壓後放到eclipse安裝目錄下的plugins目錄,重啓eclipse服務器,File new ->other,可以看到下面的這兩個選項,表示插件安裝成功


二、Axis2服務端開發

1、在tomcat中部署axis2(爲了接下來發布webservice服務)

下載axis2-1.7.6-war.zip,解壓到tomcat的webapps目錄下,啓動tomcat服務器,會發現webapps目錄下多了axis2文件夾,在瀏覽器裏輸入http://localhost:8080/axis2,會發現對應的網頁,說明已經成功了

2、建立要發佈的WebService 

   2.1、new java project:AxisService

2.2、編寫需要發佈的WebService,在src目錄下建包samples.quickstart.service.pojo,new class StockQuoteService如下

package samples.quickstart.service.pojo;

import java.util.HashMap;
import java.util.Map.Entry;

public class StockQuoteService {
	private HashMap<String, Double> map = new HashMap<String, Double>();

	public double getPrice(String symbol) {
		Double price = (Double) map.get(symbol);
		if (price != null) {
			return price.doubleValue();
		}
		return 42.00;
	}

	public String list() {
		String result = "{";
		for (Entry<String, Double> entry : map.entrySet()) {
			result += entry.getKey() + ":" + entry.getValue() + ",";
		}
		result += "}";
		return result;
	}

	public void update(String symbol, double price) {
		map.put(symbol, new Double(price));
	}

	public String sayHello(String name) {
		return "hello" + name + "axis2";
	}
}


3、發佈WebService

3.1、打包要發佈的Service, 點擊Eclipse中New -> File -> Other -> Axis2 wizards -> Axis2 Services Archiver

3.2、按上圖填寫,class File Location爲工作目錄對應項目的bin文件夾,並勾上Include .class files only,點擊next

3.3、默認選擇Skip WSDL,點擊next

3.4、默認,繼續next

3.5、默認,繼續next

3.6、如上圖所示,選擇正確的Class Name,否則load不到class

3.7 、如上圖所示,output file location填寫tomcat目錄的axis2\web-inf\services下,點擊Finish後,可以發現:F:\apache-tomcat-6.0.45\webapps\axis2\WEB-INF\services目錄下增加了StockQuoteService.aar

3.8、測試發佈的WebService

 打開http://localhost:8080/axis2/services/listServices頁面,可以看到所發佈的服務:

點擊StockQuoteService鏈接查看wsdl

webservice發佈成功。

三、Axis2客戶端開發

1、File-new 選擇Axis2 Code Generator 點擊next

2、選擇從wsdl生成java文件,點擊next

3、選擇生成好的wsdl文件

4、Codegen option選擇default,點擊next

5、選擇將生成的java代碼保存到工作空間的特定工程裏面

生成的客戶端代碼如下

添加axis2需要的jar包到工程裏面。不然會報錯,具體需要的jar從axis2-1.7.6-bin.zip這個下載包裏面的lib目錄去取。

新建包test,然後new class StockQuoteServiceTest,代碼如下:

package test;

import java.rmi.RemoteException;

import samples.quickstart.service.pojo.GetPrice;
import samples.quickstart.service.pojo.GetPriceResponse;
import samples.quickstart.service.pojo.ListResponse;
import samples.quickstart.service.pojo.SayHello;
import samples.quickstart.service.pojo.StockQuoteServiceStub;
import samples.quickstart.service.pojo.Update;

public class StockQuoteServiceTest {
    public static void main(java.lang.String args[]){
        try{
            StockQuoteServiceStub stub =
                new StockQuoteServiceStub
                ("http://localhost:8080/axis2/services/StockQuoteService?wsdl");

            getPrice(stub);
            update(stub);
            list(stub);
            getPrice(stub);
            sayHello(stub);
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }

    /* fire and forget */
    public static void update(StockQuoteServiceStub stub){
        try{
        	Update update = new Update();
        	update.setSymbol("ABC");
        	update.setPrice(43.35);
            stub.update(update);
            
        	update.setSymbol("CDE");
        	update.setPrice(12.00);
            stub.update(update);
            
            
            System.err.println("price updated");
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }

    /* two way call/receive */
    public static void getPrice(StockQuoteServiceStub stub){
        try{
        	GetPrice price = new GetPrice();
        	price.setSymbol("ABC");
        	GetPriceResponse response = stub.getPrice(price);
            System.err.println(response.get_return());
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
    
    public static void list(StockQuoteServiceStub stub){
    	try {
			ListResponse listResponse = stub.list();
			System.out.println(listResponse.get_return());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public static void sayHello(StockQuoteServiceStub stub){
    	SayHello sayHello = new SayHello();
    	sayHello.setName("weir");
    	try {
			System.out.println(stub.sayHello(sayHello).get_return());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
 
}
運行代碼結果如下:


update 方法執行無效,價格沒設置進去,這個問題還得查資料。

使用Axis2框架實現webservice就先寫到這邊了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章