標籤:
1.安裝配置axis2環境
1)下載axis2-1.4.1-war(發布webservice)和axis2-1.4.1-bin.zip(webservice調用使用的各種包)
下載好後把axis2-1.4.1-war目錄下面的axis2.war發布到tomcat的webapps中。
發布好,訪問:http://localhost:8079/axis2/ 介面如下:
2.開發web服務
1)建立一個java web project
2)編寫服務代碼
1 public class SampleService { 2 3 public String getGreeting(String user){ 4 return "你好"+user; 5 } 6 7 public int getPrice() { 8 return new java.util.Random().nextInt(100); 9 }10 }
注意:工程中的SampleService.java不能有package和import語句
3.發布服務
運行改工程得到SampleService.class檔案,然後將其拷貝到%TOMECAT_EHOME%\webapps\axis2\WEB-INF\pojo下, 如果WEB-INF目錄下面沒有pojo這個目錄,那你得必須建立一個,因為在axis2的設定檔中配置從pojo目錄發布服務
4.查看服務
發布好後,啟動你的tomcat,訪問http://localhost:8079/axis2/services/listServices 看是否發布成功,介面如下:
看到我們剛發布的SampleService說明成功了,接下來可以通過下面的連結訪問服務了
http://localhost:8079/axis2/services/SimpleService/getGreeting?name=bill
http://localhost:8079/axis2/services/SimpleService/getPrice
5.client調用服務
1)產生服務的client代碼
2)建立一個client項目,將產生的程式碼拷貝進去
3)建立一個Test類
1 package com.test; 2 3 import com.axis2.client.SampleServiceStub; 4 5 public class SampleServiceTest { 6 7 public static void main(String[] args) throws Exception { 8 SampleServiceStub client=new SampleServiceStub(); 9 10 SampleServiceStub.GetGreeting gg=new SampleServiceStub.GetGreeting();11 gg.setUser("wangfang");12 13 System.out.println(client.getGreeting(gg).get_return());14 System.out.println(client.getPrice().get_return());15 16 }17 }
注意將axis2-1.4.1-bin\lib下的jar包導進去
3)運行查看效果
基於Apache axis2開發Java Web服務