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 都市淘沙者 阅读(2267) 评论(0)  编辑  收藏 所属分类: AJAX/XML/ANT/SOAP/WEBService


只有注册用户登录后才能发表评论。


网站导航: