CXF开发WebService客户端

开发必备
1.apache-cxf-2.2.6
2.spring-ws-1.5.8
3.eclipse-jee-galileo-SR1-win32

开发步骤:
一、新建一个普通的java工程,名字叫WebService_CXF_Client
二、导入apache-cxf-2.2.6 及 spring-ws-1.5.8 下的jar包
三、启动《CXF开发WebService服务器端》所开发的WebService服务器
四、进入apache-cxf-2.2.6/bin文件夹内,运行
wsdl2java.bat http://localhost:8080/WebService_CXF_Host/service/HelloWorld?wsdl
将会在bin文件夹内生成一个demo文件夹
注意,可以查看demo.sprint文件夹内的HelloWorld.java接口,定义的aryMyObjects方法的返回值是List<MyObject>类型,尽管在服务器端定义接口的返回值类型是数组.
五、将整个demo文件夹剪切到WebService_CXF_Client工程的src目录下
六、在src目录下新建一个client-beans.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
[color=red]xmlns:jaxws="http://cxf.apache.org/jaxws"[/color]
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
[color=red]http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd[/color]">

<bean id="client" class="demo.spring.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.HelloWorld"/>
<property name="address" value="http://localhost:8080/WebService_CXF_Host/service/HelloWorld"/>
</bean>

<jaxws:client id="client2"
serviceClass="demo.spring.HelloWorld"
address="http://localhost:8080/WebService_CXF_Host/service/HelloWorld" />
</beans>

注意:client 与 client2 是两种访问webservice的写法,效果一样。

七、新建含main方法的Client.java类,内容中下:

import java.util.ArrayList;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.spring.HelloWorld;
import demo.spring.MyObject;


public final class Client {

private Client() {
}

public static void main(String args[]) throws Exception {
// START SNIPPET: client
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});

HelloWorld client = (HelloWorld)context.getBean("client2");

System.out.println("begin stringInt method");
int num=999;
String text="i love you";
String response = client.stringInt(text, num);
System.out.println("Response: " + response);

System.out.println("begin aryMyObjects method");
List<MyObject> lstSource=new ArrayList<MyObject>();
MyObject myObject1=new MyObject();
myObject1.setId(0);
myObject1.setName("000");
MyObject myObject2=new MyObject();
myObject2.setId(1);
myObject2.setName("111");
lstSource.add(myObject1);
lstSource.add(myObject2);

List<MyObject> lstResponse=client.aryMyObjects(lstSource);
for(int i=0;i<lstResponse.size();i++){
System.out.println("id: "+lstResponse.get(i).getId()+" name: "+lstResponse.get(i).getName());
}

System.exit(0);
}
}

[color=red]至此,客户端也成功开发了。[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值