微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

webservice Apache axis2 adb之開發實例

1.配apache axis2 運行環境

a.從apache  axis2官方網站下載 axis2.war,axis2-1.4.1-bin.zip

b.配制axis2_home,path,class_path環境變量

2.在eclipse新建一個java project ,在工程中根據向導創建getUserInfo.wsdl. 

 

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:deFinitions name="getUserInfo" targetNamespace="http://www.example.org/getUserInfo/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://www.example.org/getUserInfo/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://www.example.org/getUserInfo/">
      <xsd:element name="getUser">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element  name="id" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="getUserResponse">
        <xsd:complexType>
          <xsd:sequence>
           <xsd:element name="name" type="xsd:string"/>
           <xsd:element name="dept" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="getUserResponse">
    <wsdl:part name="parameters" element="tns:getUserResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserRequest">
    <wsdl:part name="parameters" element="tns:getUser">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="getUserInfo">
    <wsdl:operation name="getUser">
      <wsdl:input message="tns:getUserRequest">
    </wsdl:input>
      <wsdl:output message="tns:getUserResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="getUserInfoSOAP" type="tns:getUserInfo">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getUser">
      <soap:operation soapAction="http://www.example.org/getUserInfo/getUser"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="getUserInfo">
    <wsdl:port name="getUserInfoSOAP" binding="tns:getUserInfoSOAP">
      <soap:address location="http://www.example.org/"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:deFinitions>

3. 使用axis2自帶的工具wsdl2ajva 生成客戶端和服務端代碼

命令:wsdl2java -uri getUserInfo.wsdl  -o bulid -s -ss -ssi -sd -d adb 可以生成服務端代碼

命令:wsdl2java -uri getUserInfo.wsdl  -o bulid  -d adb -s 可以生成客戶端代碼

4.修改服務器端的類

 

package org.example.www.getuserinfo;

public class GetUserInfoSkeleton implements GetUserInfoSkeletonInterface {

 /**
  * Auto generated method signature
  *
  * @param getUser0
  */

 public GetUserResponse getUser(
   GetUser getUser0) {
  GetUserResponse res = new GetUserResponse();
      if (getUser0.getId().endsWith("001")){
  res.setDept("IT");
  res.setName("APACHE");
      }
      System.out.println(getUser0.getId());
  return res;
 }

}

在這里也可以填寫您自已的業務羅輯 ,註意返回是GetUserResponse 的對象
5.修改services.xml中部份代碼,為紅色部份

<?xml version="1.0" encoding="UTF-8"?>
<!-- This file was auto-generated from WSDL -->
<!-- by the Apache Axis2 version: 1.4.1  Built on : Aug 13,2008 (05:03:35 LKT) -->
<serviceGroup>
    <service name="getUserInfo" scope="application" >
        <messageReceivers>
            <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" class="org.example.www.getuserinfo.GetUserInfoMessageReceiverInOut"/>
        </messageReceivers>
        <parameter name="ServiceClass">org.example.www.getuserinfo.GetUserInfoSkeleton</parameter>
        <parameter name="uSEOriginalwsdl">false</parameter>
        <parameter name="modifyUserWSDLPortAddress">true</parameter>
        <operation name="getUser" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://www.example.org/getUserInfo/">
            <actionMapping>http://www.example.org/getUserInfo/getUser</actionMapping>
            <outputActionMapping>http://www.example.org/getUserInfo/getUserInfo/getUserResponse</outputActionMapping>
        </operation>
    </service>
</serviceGroup>

 

6.產生getUserInfo.aar文件

將服務端中org.example.www.getuserinfo包下所有的class 文件meta-inf/services.xml 拷貝到一個目錄下

在cmd 進入這個目錄,並執行命令:jar cvf  getUserInfo.aar .

註意目錄結構。

7. 將發布到 getUserInfo.aar 到tomcat 中webapps/axis2/WEB-INF/services目錄下,啟動tomcat ,輸入網址http://localhost:8080/axis2/services/listServices

可以看到webservice 了。

8.客戶端調用程式

package org.example.www.getuserinfo;

import java.rmi.remoteexception;

import org.apache.axis2.AxisFault;

public class client {
 public static void main(String[] args) throws remoteexception,AxisFault {

  GetUserInfoStub stub = new GetUserInfoStub(
    "http://localhost:8080/axis2/services/getUserInfo");
  GetUserInfoStub.GetUser user = new GetUserInfoStub.GetUser();
  user.setId("001");
     GetUserInfoStub.GetUserResponse res = stub.getUser(user);
  System.out.println(res.getDept());
  System.out.println(res.getName());

 }

}

9.運行結果

IT

APACHE

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐