Dict.CN 在線詞典, 英語學習, 在線翻譯

          都市淘沙者

          荔枝FM Everyone can be host

          統計

          留言簿(23)

          積分與排名

          優秀學習網站

          友情連接

          閱讀排行榜

          評論排行榜

          SOAP中復雜類型(JavaBean)調用實例實踐(轉)

            1SOAP中復雜類型(JavaBean)調用實例實踐  2
            3 
            4
            5使用工具:axis-1_1
            6
            7                Tomcat 5.2.x
            8
            9IDE: Eclipse 3.1
           10
           11 
           12
           13一、簡單開始:
           14
           151、創建一個JavaBean類     Student.java
           16
           17package com.kevinGQ.service.axis.model;
           18
           19 
           20
           21import java.io.Serializable;
           22
           23 
           24
           25public class Student implements Serializable{
           26
           27       private String _name;
           28
           29       private String _id;
           30
           31       private String _comment;
           32
           33 
           34
           35       public Student(){}
           36
           37       
           38
           39       public Student(String name, String id, String comment){
           40
           41              _name = name;
           42
           43              _id = id;
           44
           45              _comment = comment;
           46
           47       }
           48
           49       
           50
           51       public String getName(){
           52
           53              return _name;
           54
           55       }
           56
           57       
           58
           59       public void setName(String name){
           60
           61              _name = name;
           62
           63       }
           64
           65       
           66
           67       public String getId(){
           68
           69              return _id;
           70
           71       }
           72
           73       
           74
           75       public void setId(String id){
           76
           77              _id = id;
           78
           79       }
           80
           81       
           82
           83       public String getComment(){
           84
           85              return _comment;
           86
           87       }
           88
           89       
           90
           91       public void setComment(String comment){
           92
           93              _comment = comment;
           94
           95       }
           96
           97}
           98
           99 
          100
          1012、寫Service程序
          102
          103package com.kevinGQ.service.axis.service;
          104
          105 
          106
          107import com.kevinGQ.service.axis.model.Student;
          108
          109 
          110
          111public class GetStudentService {
          112
          113       public Student getAStudent(String name){
          114
          115              Student a = new Student("a","10001","I'm A");
          116
          117              return a;
          118
          119       }
          120
          121}
          122
          123 
          124
          1253、部署axis及部署service
          126
          127a. 從axis-1_1.zip中將axis-1_1/webapps/axis 文件夾拷貝到Tomcat 5.0.x/webapps/
          128
          129b. 打開webapps/axis/WEB-INF/server-config.wsdd進行編輯,在<deployment>標簽下插入如下片斷
          130
          131<service name="StudentInfoService" provider="java:RPC">
          132
          133        <parameter name="className" value="com.kevinGQ.service.axis.service.GetStudentService"/>
          134
          135        <parameter name="allowedMethods" value="*"/>
          136
          137       <beanMapping qname="myNS:Student" xmlns:myNS="urn:StudentInfoService" languageSpecificType="java:com.kevinGQ.service.axis.model.Student"/>
          138
          139 </service>
          140
          141片斷中StudentInfoService是這個web service的名字,在客戶端編碼的時候需要用到。
          142
          143<parameter name="className" value="com.kevinGQ.service.axis.service.GetStudentService"/>
          144
          145中說明了這個服務提供的類,包括package的完整類名。
          146
          147<parameter name="allowedMethods" value="*"/>中說明這個服務中可供給外部調用的方法有哪些,*表示全部函數,現在也可以把*改成getAStudent.
          148
          149<beanMapping qname="myNS:Student" xmlns:myNS="urn:StudentInfoService" languageSpecificType="java:com.kevinGQ.service.axis.model.Student"/>中說明對于這個JavaBean的傳輸需要如何對它進行serializing和de-serializing,說明的目的在于綁定JavaBean的對象類別。注意標簽中說明的名字空間。這個標簽其實是如下標簽的一個簡寫:
          150
          151<typeMapping qname="myNs:Student" xmlns:ns="urn:StudentInfoService"
          152
          153             languageSpecificType="java: com.kevinGQ.service.axis.model.Student "
          154
          155             serializer="org.apache.axis.encoding.ser.BeanSerializerFactory "
          156
          157             deserializer=" org.apache.axis.encoding.ser.BeanDeserializerFactory "
          158
          159             encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
          160
          161 
          162
          163c. 把編譯好的Student.class 和 GetStudentService.class(在它們各自的包內) 放到axis/WEB-INF/classes/.
          164
          165 
          166
          1674、啟動Tomcat, 訪問http://localhost:8080/axis/admin.html,查看你部署的服務
          168
          169 
          170
          1715、編寫客戶端
          172
          173我是在Eclipse里完成代碼的編寫,編譯及運行需要把axis-1_1/lib/ 除了axis_ant.jar外7個jar文件導入到classpath.
          174
          175package com.kevinGQ.service.axis.client;
          176
          177 
          178
          179import java.net.URL;
          180
          181 
          182
          183import javax.xml.namespace.QName;
          184
          185import javax.xml.rpc.ParameterMode;
          186
          187 
          188
          189import org.apache.axis.client.Call;
          190
          191import org.apache.axis.client.Service;
          192
          193import org.apache.axis.encoding.XMLType;
          194
          195import org.apache.axis.encoding.ser.BeanDeserializerFactory;
          196
          197import org.apache.axis.encoding.ser.BeanSerializerFactory;
          198
          199 
          200
          201import com.kevinGQ.service.axis.model.Student;
          202
          203 
          204
          205public class GetAStudentClient {
          206
          207       public static void main(String [] args) throws Exception
          208
          209    {
          210
          211              Service service = new Service();
          212
          213        Call call = (Call) service.createCall();
          214
          215        QName qn = new QName("urn:StudentInfoService","Student");
          216
          217        call.registerTypeMapping(Student.class,qn,
          218
          219                      new BeanSerializerFactory(Student.class, qn),
          220
          221                            new BeanDeserializerFactory(Student.class, qn)
          222
          223        );
          224
          225        try{
          226
          227               call.setTargetEndpointAddress(new URL("http://localhost:8080/axis/services/StudentService"));
          228
          229               call.setOperationName(new QName("StudentInfoService","getAStudent"));
          230
          231               call.addParameter("arg1",XMLType.XSD_STRING, ParameterMode.IN);
          232
          233               call.setReturnClass(Student.class);
          234
          235               Student a = (Student) call.invoke(new Object[]{"a"});
          236
          237               System.out.println(a.getId());
          238
          239        }catch(Exception e) {
          240
          241            System.out.println( "Error : " + e.toString());
          242
          243        }        
          244
          245    }
          246
          247}
          248
          249紅色代碼部分表明任意的一個字符串,因為getAStudent方法的參數對返回的結果沒有影響,這里只是象征性的傳遞一個參數。加粗的部分是需要在Client端說明要serialize和de-serialize的JavaBean類別,參數的說明可參考axis api 文檔。
          250
          251要得到運行的結果,客戶端這邊需要得到Student.class文件,可是如果對于一個不在本機的服務,如何得到這個Student.class呢?——你需要閱讀一下這個WebService的wsdl文檔,里面有對這個JavaBean對象中各個域的說明,根據JavaBean的編碼規范,你自己編寫編譯就得到了Student.class文件。
          252
          253 
          254
          255二、稍微深入
          256
          257我想得到的是一個Student的數組怎么辦呢?
          258
          259你只有稍做修改:
          260
          2611、服務端的一個新類 StudentLib.java
          262
          263package com.kevinGQ.service.axis.model;
          264
          265 
          266
          267import java.util.ArrayList;
          268
          269 
          270
          271public class StudentLib {
          272
          273       ArrayList studentLib = null;
          274
          275       
          276
          277       public StudentLib(){
          278
          279              studentLib = new ArrayList();
          280
          281       }
          282
          283       
          284
          285       public void addStudent(Student s){
          286
          287              studentLib.add(s);
          288
          289       }
          290
          291       
          292
          293       public ArrayList getStudents(String name, String id){
          294
          295              ArrayList list = new ArrayList();
          296
          297              for(int i = 0; i < studentLib.size(); i++){
          298
          299                     if(this.get(i).getName().equals(name) 
          300
          301                            && this.get(i).getId().equals(id)){
          302
          303                                   list.add(this.get(i));
          304
          305                            }
          306
          307              }
          308
          309              return list;
          310
          311       }
          312
          313       
          314
          315       public Student get(int index){
          316
          317              return (Student)studentLib.get(index);
          318
          319       }
          320
          321}
          322
          323這個類只不過是為了實現稍微復雜點的邏輯功能而寫。注意getStudents方法返回的是ArrayList類型的引用。因為SOAP中支持的數據類型包含java中ArrayList,所以用這個類型會方便很多。
          324
          325 
          326
          3272、擴展Service程序
          328
          329package com.kevinGQ.service.axis.service;
          330
          331 
          332
          333import java.util.ArrayList;
          334
          335 
          336
          337import com.kevinGQ.service.axis.model.Student;
          338
          339import com.kevinGQ.service.axis.model.StudentLib;
          340
          341 
          342
          343public class GetStudentService {
          344
          345       public ArrayList getStudent(){
          346
          347              ArrayList students = new ArrayList();
          348
          349              Student a = new Student("a","10001","I'm A");
          350
          351              Student b = new Student("a","10002","I'm B");
          352
          353              Student c = new Student("a","10001","I'm A, I'm not C");
          354
          355              StudentLib lib = new StudentLib();
          356
          357              lib.addStudent(a);
          358
          359              lib.addStudent(b);
          360
          361              lib.addStudent(c);
          362
          363              
          364
          365              students = lib.getStudents("a","10001");
          366
          367              return students;
          368
          369       }
          370
          371       public Student getAStudent(String name){
          372
          373              Student a = new Student("a","10001","I'm A");
          374
          375              return a;
          376
          377       }
          378
          379}
          380
          381加粗的地方為添加的新的方法,我們接著要在服務端描述它
          382
          383 
          384
          3853、部署service
          386
          387把剛才添加到server-config.wsdd的那個片斷再拿出來看看,好像不用修改(只要你在allowedMethods的地方表明允許暴露的方法的是*)
          388
          389 
          390
          3914、寫個客戶端看看
          392
          393package com.kevinGQ.service.axis.client;
          394
          395 
          396
          397import java.net.URL;
          398
          399import java.util.ArrayList;
          400
          401 
          402
          403import org.apache.axis.client.Call;
          404
          405import org.apache.axis.client.Service;
          406
          407import org.apache.axis.encoding.XMLType;
          408
          409import org.apache.axis.encoding.ser.BeanDeserializerFactory;
          410
          411import org.apache.axis.encoding.ser.BeanSerializerFactory;
          412
          413 
          414
          415import com.kevinGQ.service.axis.model.Student;
          416
          417 
          418
          419import javax.xml.namespace.QName;
          420
          421import javax.xml.rpc.ParameterMode;
          422
          423 
          424
          425public class GetStudentClient {
          426
          427       
          428
          429       public static void main(String [] args) throws Exception
          430
          431    {
          432
          433              Service service = new Service();
          434
          435        Call call = (Call) service.createCall();
          436
          437        QName qn = new QName("urn:StudentInfoService","Student");
          438
          439        call.registerTypeMapping(Student.class,qn,
          440
          441                      new BeanSerializerFactory(Student.class, qn),
          442
          443                            new BeanDeserializerFactory(Student.class, qn));
          444
          445        try{
          446
          447               call.setTargetEndpointAddress(new URL("http://localhost:8080/axis/services/StudentService"));
          448
          449               call.setOperationName(new QName("StudentInfoService","getStudent"));
          450
          451;
          452
          453               call.setReturnClass(ArrayList.class);
          454
          455               ArrayList result = (ArrayList) call.invoke(new Object[]{});
          456
          457               for(int i = 0; i < result.size(); i++){
          458
          459                      Student stu = (Student)result.get(i);
          460
          461                      System.out.println(stu.getName()+" "+stu.getId()+" "+stu.getComment());
          462
          463               }
          464
          465        }catch(Exception e) {
          466
          467            System.out.println( "Error : " + e.toString());
          468
          469        }        
          470
          471    }
          472
          473}
          474
          475和第一個客戶端很相似吧。注意把Call返回的類型設為ArrayList,看代碼中加粗部分!
          476
          477結果輸出了2條記錄,和預期的一樣。要不,你試試。
          478
          479 
          480
          481 
          482
          483附:文中描述服務的wsdl.xml
          484<?xml version="1.0" encoding="UTF-8"?>
          485<wsdl:definitions targetNamespace="http://localhost:8080/axis/services/StudentInfoService" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/services/StudentInfoService" xmlns:intf="http://localhost:8080/axis/services/StudentInfoService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="urn:StudentInfoService" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          486  <wsdl:types>
          487    <schema targetNamespace="urn:StudentInfoService" xmlns="http://www.w3.org/2001/XMLSchema">
          488      <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
          489      <complexType name="Student">
          490         <sequence>
          491           <element name="comment" nillable="true" type="xsd:string"/>
          492           <element name="name" nillable="true" type="xsd:string"/>
          493           <element name="id" nillable="true" type="xsd:string"/>
          494         </sequence>
          495      </complexType>
          496    </schema>
          497  </wsdl:types>
          498  <wsdl:message name="getAStudentRequest">
          499    <wsdl:part name="name" type="xsd:string"/>
          500  </wsdl:message>
          501  <wsdl:message name="getAStudentResponse">
          502    <wsdl:part name="getAStudentReturn" type="tns1:Student"/>
          503  </wsdl:message>
          504  <wsdl:message name="getStudentResponse">
          505    <wsdl:part name="getStudentReturn" type="soapenc:Array"/>
          506  </wsdl:message>
          507  <wsdl:message name="getStudentRequest">
          508  </wsdl:message>
          509  <wsdl:portType name="GetStudentService">
          510    <wsdl:operation name="getStudent">
          511      <wsdl:input message="impl:getStudentRequest" name="getStudentRequest"/>
          512      <wsdl:output message="impl:getStudentResponse" name="getStudentResponse"/>
          513    </wsdl:operation>
          514    <wsdl:operation name="getAStudent" parameterOrder="name">
          515      <wsdl:input message="impl:getAStudentRequest" name="getAStudentRequest"/>
          516      <wsdl:output message="impl:getAStudentResponse" name="getAStudentResponse"/>
          517    </wsdl:operation>
          518  </wsdl:portType>
          519  <wsdl:binding name="StudentInfoServiceSoapBinding" type="impl:GetStudentService">
          520    <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
          521    <wsdl:operation name="getStudent">
          522      <wsdlsoap:operation soapAction=""/>
          523      <wsdl:input name="getStudentRequest">
          524        <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://service.axis.service.kevinGQ.com" use="encoded"/>
          525      </wsdl:input>
          526      <wsdl:output name="getStudentResponse">
          527        <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/services/StudentInfoService" use="encoded"/>
          528      </wsdl:output>
          529    </wsdl:operation>
          530    <wsdl:operation name="getAStudent">
          531      <wsdlsoap:operation soapAction=""/>
          532      <wsdl:input name="getAStudentRequest">
          533        <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://service.axis.service.kevinGQ.com" use="encoded"/>
          534      </wsdl:input>
          535      <wsdl:output name="getAStudentResponse">
          536        <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/services/StudentInfoService" use="encoded"/>
          537      </wsdl:output>
          538    </wsdl:operation>
          539  </wsdl:binding>
          540  <wsdl:service name="GetStudentServiceService">
          541    <wsdl:port binding="impl:StudentInfoServiceSoapBinding" name="StudentInfoService">
          542      <wsdlsoap:address location="http://localhost:8080/axis/services/StudentInfoService"/>
          543    </wsdl:port>
          544  </wsdl:service>
          545</wsdl:definitions>
          546

          posted on 2007-12-12 12:14 都市淘沙者 閱讀(2275) 評論(0)  編輯  收藏 所屬分類: AJAX/XML/ANT/SOAP/WEBService

          主站蜘蛛池模板: 彩票| 台东县| 长沙县| 新乐市| 临城县| 中卫市| 鹤山市| 盐山县| 汕尾市| 青阳县| 凭祥市| 吴江市| 商水县| 疏附县| 新巴尔虎左旗| 长乐市| 嘉荫县| 芦溪县| 南丰县| 台州市| 法库县| 清徐县| 应用必备| 榕江县| 南丰县| 浠水县| 鹤岗市| 甘德县| 双牌县| 淮安市| 温泉县| 沾化县| 海兴县| 宜丰县| 建水县| 乌兰察布市| 永德县| 万荣县| 宁陵县| 永胜县| 旬邑县|