SOAP中復雜類型(JavaBean)調用實例實踐(轉)
1
SOAP中復雜類型(JavaBean)調用實例實踐 2
3
4
5
使用工具:axis-1_1
6
7
Tomcat 5.2.x
8
9
IDE: Eclipse 3.1
10
11
12
13
一、簡單開始:
14
15
1、創建一個JavaBean類 Student.java
16
17
package com.kevinGQ.service.axis.model;
18
19
20
21
import java.io.Serializable;
22
23
24
25
public 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
101
2、寫Service程序
102
103
package com.kevinGQ.service.axis.service;
104
105
106
107
import com.kevinGQ.service.axis.model.Student;
108
109
110
111
public 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
125
3、部署axis及部署service
126
127
a. 從axis-1_1.zip中將axis-1_1/webapps/axis 文件夾拷貝到Tomcat 5.0.x/webapps/
128
129
b. 打開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
163
c. 把編譯好的Student.class 和 GetStudentService.class(在它們各自的包內) 放到axis/WEB-INF/classes/.
164
165
166
167
4、啟動Tomcat, 訪問http://localhost:8080/axis/admin.html,查看你部署的服務
168
169
170
171
5、編寫客戶端
172
173
我是在Eclipse里完成代碼的編寫,編譯及運行需要把axis-1_1/lib/ 除了axis_ant.jar外7個jar文件導入到classpath.
174
175
package com.kevinGQ.service.axis.client;
176
177
178
179
import java.net.URL;
180
181
182
183
import javax.xml.namespace.QName;
184
185
import javax.xml.rpc.ParameterMode;
186
187
188
189
import org.apache.axis.client.Call;
190
191
import org.apache.axis.client.Service;
192
193
import org.apache.axis.encoding.XMLType;
194
195
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
196
197
import org.apache.axis.encoding.ser.BeanSerializerFactory;
198
199
200
201
import com.kevinGQ.service.axis.model.Student;
202
203
204
205
public 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
261
1、服務端的一個新類 StudentLib.java
262
263
package com.kevinGQ.service.axis.model;
264
265
266
267
import java.util.ArrayList;
268
269
270
271
public 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
327
2、擴展Service程序
328
329
package com.kevinGQ.service.axis.service;
330
331
332
333
import java.util.ArrayList;
334
335
336
337
import com.kevinGQ.service.axis.model.Student;
338
339
import com.kevinGQ.service.axis.model.StudentLib;
340
341
342
343
public 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
385
3、部署service
386
387
把剛才添加到server-config.wsdd的那個片斷再拿出來看看,好像不用修改(只要你在allowedMethods的地方表明允許暴露的方法的是*)
388
389
390
391
4、寫個客戶端看看
392
393
package com.kevinGQ.service.axis.client;
394
395
396
397
import java.net.URL;
398
399
import java.util.ArrayList;
400
401
402
403
import org.apache.axis.client.Call;
404
405
import org.apache.axis.client.Service;
406
407
import org.apache.axis.encoding.XMLType;
408
409
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
410
411
import org.apache.axis.encoding.ser.BeanSerializerFactory;
412
413
414
415
import com.kevinGQ.service.axis.model.Student;
416
417
418
419
import javax.xml.namespace.QName;
420
421
import javax.xml.rpc.ParameterMode;
422
423
424
425
public 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


3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

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