基於Myeclipse+Axis2的WebService開發實錄

來源:互聯網
上載者:User

標籤:pre   sage   arp   return   init   location   orm   iter   pack   

  最近開始學習了下在Myeclipse開發工具下基於WebSerivce的開發,下面將相關相關關鍵資訊予以記錄

  1. Myeclipse的安裝,本文以Myeclipse2014-blue為開發環境,相關配置執行完善
  2. 從http://archive.apache.org/dist/ws/axis2/tools/下載Axis2包,下載 axis2-eclipse-codegen-wizard.zip,下載axis2-eclipse-service-archiver-wizard.zip
  3. 從http://axis.apache.org/axis2/java/core/download.html下載(axis2-1.7.0-bin.zip,axis2-1.7.0-war.zip,axis2-eclipse-codegen-plugin-1.7.0.zip,axis2-eclipse-service-plugin-1.7.0.zip)
  4. 下載說明:從步驟1下載出來的Axis2_Codegen_Wizard_1.3.0檔案夾,Axis2_Service_Archiver_1.3.0檔案夾拷貝到myeclipse安裝目錄之dropins檔案下,重啟myeclipse下載到的axis2-1.7.0-war.zip,拷貝檔案之tomact/webapps目錄下面,重新命名axis2.war,重新整理檔案夾,tomact為解壓該部署套件,此時可以通過tomact服務訪問axis2站,看到相關頁面表示成功
  5. 在步驟4中,注意檔案夾axis2\WEB-INF,下面有3個檔案夾,conf,modules,services,lib檔案夾,稍後我們會用到
  6. 下面藉助於axis2來開發webservice
  7. 開啟myeclipse建立web project項目,將步驟5中conf,modules,services,lib檔案夾的拷貝之WebRoot/WEB-INF/下面,並在WEB-INF下面建立Web.xml檔案,倘若web.xml存在則開啟,添加以下代碼配置axis2
  8. axis2配置代碼如下:
        <!-- 載入Axis2 -->  <servlet>  <servlet-name>AxisServlet</servlet-name>  <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>AxisServlet</servlet-name>  <url-pattern>/services/*</url-pattern>  </servlet-mapping> 

      

  9. 在WEB-INF/services/下建立Axis2Service/META-INF/目錄,建立檔案services.xml,添加如下webservice服務配置代碼
        <!-- HelloWorld表示您的WebService服務名 --><service name="HelloWorld" >    <!-- HelloWorld表示您的WebService服務描述 -->    <description>            HelloWorld  Service Example       </description>      <!-- 這個必須是這個服務的類路徑 -->    <parameter name="ServiceClass">              cn.homily.action.HelloWorld    </parameter>      <!-- 這個是這個服務的方法名 -->    <operation name="getHello">            <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />        </operation>        <operation name="getWorld">            <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />        </operation>        <operation name="getHelloWorld">    <!-- 這裡要注意,當沒有傳回值時才用   org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver,沒有參數還是用RPCMessageReceiver-->          <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />        </operation> </service>

      

  10. 在src包下面添加子包cn.homily.action,添加HelloWorld.java檔案,其代碼如下:
    package cn.homily.action;//服務名public class HelloWorld{
      //服務方法 public String getHello(String name) { return "Hello, " + name + "."; } //服務方法 public String getWorld(String name) { return "World," + name + "."; } //服務方法 public String getHelloWorld() { return "Hello,World"; }}

      經過以上步驟,我們的WebService基本代碼已經全了,現在看看實際效果。

  11. 現在打包我們的Web-Project,部署我們的Web-Project至tomact,現在我們運行,在瀏覽器輸入如下地址http://zgc-20150226yxm:8080/sayHello2Axis/services/HelloWorld?wsdl。如果在瀏覽器裡面看到了以下代碼,說明我們的Web-Service服務Demo開發完成。正確的頁面效果如下:
    This XML file does not appear to have any style information associated with it. The document tree is shown below.<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://action.homily.cn" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" targetNamespace="http://action.homily.cn"><wsdl:documentation>HelloWorld</wsdl:documentation><wsdl:types>...</wsdl:types><wsdl:message name="getHelloRequest"><wsdl:part name="parameters" element="ns:getHello"/></wsdl:message><wsdl:message name="getHelloResponse"><wsdl:part name="parameters" element="ns:getHelloResponse"/></wsdl:message><wsdl:message name="getHelloWorldRequest"><wsdl:part name="parameters" element="ns:getHelloWorld"/></wsdl:message><wsdl:message name="getHelloWorldResponse"><wsdl:part name="parameters" element="ns:getHelloWorldResponse"/></wsdl:message><wsdl:message name="getWorldRequest"><wsdl:part name="parameters" element="ns:getWorld"/></wsdl:message><wsdl:message name="getWorldResponse"><wsdl:part name="parameters" element="ns:getWorldResponse"/></wsdl:message><wsdl:portType name="HelloWorldPortType"><wsdl:operation name="getHello"><wsdl:input message="ns:getHelloRequest" wsaw:Action="urn:getHello"/><wsdl:output message="ns:getHelloResponse" wsaw:Action="urn:getHelloResponse"/></wsdl:operation><wsdl:operation name="getHelloWorld"><wsdl:input message="ns:getHelloWorldRequest" wsaw:Action="urn:getHelloWorld"/><wsdl:output message="ns:getHelloWorldResponse" wsaw:Action="urn:getHelloWorldResponse"/></wsdl:operation><wsdl:operation name="getWorld"><wsdl:input message="ns:getWorldRequest" wsaw:Action="urn:getWorld"/><wsdl:output message="ns:getWorldResponse" wsaw:Action="urn:getWorldResponse"/></wsdl:operation></wsdl:portType><wsdl:binding name="HelloWorldSoap11Binding" type="ns:HelloWorldPortType"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><wsdl:operation name="getHello"><soap:operation soapAction="urn:getHello" style="document"/><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="getHelloWorld"><soap:operation soapAction="urn:getHelloWorld" style="document"/><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="getWorld"><soap:operation soapAction="urn:getWorld" style="document"/><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:binding name="HelloWorldSoap12Binding" type="ns:HelloWorldPortType"><soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><wsdl:operation name="getHello"><soap12:operation soapAction="urn:getHello" style="document"/><wsdl:input><soap12:body use="literal"/></wsdl:input><wsdl:output><soap12:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="getHelloWorld"><soap12:operation soapAction="urn:getHelloWorld" style="document"/><wsdl:input><soap12:body use="literal"/></wsdl:input><wsdl:output><soap12:body use="literal"/></wsdl:output></wsdl:operation><wsdl:operation name="getWorld"><soap12:operation soapAction="urn:getWorld" style="document"/><wsdl:input><soap12:body use="literal"/></wsdl:input><wsdl:output><soap12:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:binding name="HelloWorldHttpBinding" type="ns:HelloWorldPortType"><http:binding verb="POST"/><wsdl:operation name="getHello"><http:operation location="getHello"/><wsdl:input><mime:content type="application/xml" part="parameters"/></wsdl:input><wsdl:output><mime:content type="application/xml" part="parameters"/></wsdl:output></wsdl:operation><wsdl:operation name="getHelloWorld"><http:operation location="getHelloWorld"/><wsdl:input><mime:content type="application/xml" part="parameters"/></wsdl:input><wsdl:output><mime:content type="application/xml" part="parameters"/></wsdl:output></wsdl:operation><wsdl:operation name="getWorld"><http:operation location="getWorld"/><wsdl:input><mime:content type="application/xml" part="parameters"/></wsdl:input><wsdl:output><mime:content type="application/xml" part="parameters"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="HelloWorld"><wsdl:port name="HelloWorldHttpSoap11Endpoint" binding="ns:HelloWorldSoap11Binding"><soap:address location="http://zgc-20150226yxm:8080/sayHello2Axis/services/HelloWorld.HelloWorldHttpSoap11Endpoint/"/></wsdl:port><wsdl:port name="HelloWorldHttpSoap12Endpoint" binding="ns:HelloWorldSoap12Binding"><soap12:address location="http://zgc-20150226yxm:8080/sayHello2Axis/services/HelloWorld.HelloWorldHttpSoap12Endpoint/"/></wsdl:port><wsdl:port name="HelloWorldHttpEndpoint" binding="ns:HelloWorldHttpBinding"><http:address location="http://zgc-20150226yxm:8080/sayHello2Axis/services/HelloWorld.HelloWorldHttpEndpoint/"/></wsdl:port></wsdl:service></wsdl:definitions>

      

基於Myeclipse+Axis2的WebService開發實錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.