基於Apache axis2開發Java Web服務

來源:互聯網
上載者:User

標籤:

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服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.