实在不才啊,今天才发现jdk6里有个wsimport.exe,使用可以生成目标Web服务器的本地调用类,然后就可以在本地程序中像使用本地代码一样调用WebService了,如此一来和DotNet下的Web服务的使用方法就基本一样。(活到老学到老啊)
使用方法如下:
1、执行wsimport命令:
wsimport http://localhost:9999/AccountService?WSDL
2、将生成的代码进行打包:
jar cvf com.zywang.cxf.jar com/zywang/cxf/
3、将生成的jar包添加到项目的构建路径中,进行引用,从使用的情况看Java的Web服务和DotNet的稍有不同
我分别使用DotNet、Spring和CXF创建了三个相同功能的Web服务,以下是客户端代码片段
访问DotNet的Web服务:
/**
* @author ZYWANG 2011-4-19
*/
public static void main(String[] args) {
AccountService service = new AccountService();
AccountServiceSoap serviceSoap = service.getAccountServiceSoap();
Account account = new Account();
account.setName("Hello ZYWANG " + (new Date()));
serviceSoap.insertAccount(account);
ArrayOfAccount arrayOfAccount = serviceSoap.getAccounts();
List<Account> accounts = arrayOfAccount.getAccount();
for (Account a : accounts) {
System.out.println(a.getName());
}
}
访问Spring的Web服务:
/**
* @author ZYWANG 2011-4-19
*/
public static void main(String[] args) {
AccountService_Service service = new AccountService_Service();
AccountService accountService = service.getAccountServicePort();
Account account = new Account();
account.setName("王朝阳 by WS " + new Date());
accountService.insertAccount(account);
List<Account> accounts = accountService.getAccounts("");
for (Account account2 : accounts) {
System.out.println(account2.getName());
}
}
访问CXF的Web服务:
/**
* @author ZYWANG 2011-4-19
*/
public static void main(String[] args) {
AccountDao_Service service = new AccountDao_Service();
AccountDao accountService = service.getAccountDaoPort();
Account account = new Account();
account.setName("Hello 王朝阳 by CXF " + (new Date()));
accountService.insertAccount(account);
List<Account> list = accountService.getAccounts("");
for (Account a : list) {
System.out.println(a.getName());
}
}
因为主要是使用wsimport和jar这两个命令,所以写了个小工具来自动生成jar包,在附件中(源码可以反编译得到)
附件中还有三种Web服务的程序,供需要的朋友参考。
工具运行截图: