jee6 学习笔记 7.2 - use "wsimport" to create a command line web service client

"wsimport" is a tool from the JAX-WS module. Sine from version 2.0, JAX-WS is a standard part of the JDK, it should be installed already and available to you if you have JDK1.6 installed on your box.

"wsimport" helps to generate the classes to consume a web service. the only required information is the wsdl file. it would generate the java classes for "marshalling/unmarshalling" the SOAP request/response payload. That is, from xml to Java classes and vice versa.

Here's its usage:

Usage: wsimport [options] <WSDL_URI>

Examples:
wsimport stock.wsdl -b stock.xml -b stock.xjb
wsimport -d generated http://example.org/stock?wsdl


There're two options that are quite useful:

-p: this option let you define the package name for the generated stuff.
-keep: this option signals the tool to keep the generated source code.

Here's the command line used to invoke the tool:

wsimport -p test.jxee.ws.gen -keep http://localhost:8180/ProJee6/StudentDAOImp?wsdl


Screen shot of invoking "wsimport"

[img]http://dl.iteye.com/upload/attachment/0071/7456/b433e376-e2cf-3659-bb22-a7f6770a43b6.png[/img]

The generated java classes can then be used to create web service client:

StudentWsClient.java

package test.jxee.ws.client;

import java.util.List;

import test.jxee.ws.gen.Student;
import test.jxee.ws.gen.StudentDAOImp;
import test.jxee.ws.gen.StudentDAOImpService;

/**
* command line tester client for the StudentDAOImpService
*/
public class StudentWsClient {

public static void main(String[] a) {
System.out.println(">>> testing the StudentDAOImpService......");

// get the service port
StudentDAOImpService service = new StudentDAOImpService();
StudentDAOImp port = service.getStudentDAOImpPort();

System.out.println(">>> calling StudentDAOImp service...");

// call the service operation find
String namefilter = "jason";
List<Student> studentList = port.find(namefilter, 10);

int listsize = (studentList != null ? studentList.size() : 0);
System.out.println(">>> printing returned student list, size: " + listsize);
for(Student s : studentList) {
System.out.println("-------------------------------");
System.out.println("name: " + s.getName());
System.out.println("age: " + s.getAge());
System.out.println("mobile: " + s.getMobile());
System.out.println("created date: " + s.getCreatedDate());
}

System.out.println(">>> testing the StudentDAOImpService..done");
}
}



Here's the result running the web service client:

C:\tmp\wstest\jwtest2>java -cp C:\JwangDev\ProJee6\build\classes test.jxee.ws.client.StudentWsClient
>>> testing the StudentDAOImpService......
>>> calling StudentDAOImp service...
>>> printing returned student list, size: 1
-------------------------------
name: jason
age: 11
mobile: 02189898
created date: 2012-07-19T15:39:03+12:00
>>> testing the StudentDAOImpService..done

C:\tmp\wstest\jwtest2>
C:\tmp\wstest\jwtest2>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值