CXF之Simple客户端搭建

0.本文是基于上一篇文章CXF之Simple服务器搭建,所以跳过pom.xml和web.xml的配置部分

1.在客户端编写一个和服务端的接口一样的接口类

package com.tangjun.ws.cxf.service.interfaces;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
	public String sayHi();
}

当然,如果不跨平台的话。其实也可以直接把服务端的类直接拿过来。


2.客户端调用服务

客户端调用服务有很多种方式,但总的看来有两大思路:

其一,通过JaxWsProxyFactoryBean来获取服务接口类,这种方法还分通过spring配置来获取和通过java代码来获取

其二,通过jaxws:client标签来自动生成

下面主要说三种方式:

a.spring配置jaxws:client

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">

	<jaxws:client id="helloWorldClient" serviceClass="com.tangjun.ws.cxf.service.interfaces.HelloWorld" 
	 	address="http://localhost:9090/exmaple_cxf_server/services/helloWorld" />

</beans>
<jaxws:client>标签会自动生成一个bean,调用的时候直接根据id来获取bean就可以了
package com.tangjun.ws.cxf.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tangjun.ws.cxf.service.interfaces.HelloWorld;

public class JaxWsClientTest {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context 
        = new ClassPathXmlApplicationContext(new String[] {"spring-cxf-client.xml"});
		
		HelloWorld service = (HelloWorld) context.getBean("helloWorldClient");
		
		System.out.println(service.sayHi());
	}
}

b.spring配置serviceProxy

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">

	<jaxws:client id="helloWorldClient" serviceClass="com.tangjun.ws.cxf.service.interfaces.HelloWorld" 
	 	address="http://localhost:9090/exmaple_cxf_server/services/helloWorld" />
	 	
	<bean id="serviceProxy" class="com.tangjun.ws.cxf.service.interfaces.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.tangjun.ws.cxf.service.interfaces.HelloWorld"/>
        <property name="address" value="http://localhost:9090/exmaple_cxf_server/services/helloWorld"/>
    </bean>
</beans>

调用的时候根据ID来获取bean就可以了

package com.tangjun.ws.cxf.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tangjun.ws.cxf.service.interfaces.HelloWorld;

public class SpringBeanTest {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context 
        = new ClassPathXmlApplicationContext(new String[] {"spring-cxf-client.xml"});
		
		HelloWorld service = (HelloWorld) context.getBean("serviceProxy");
		System.out.println(service.sayHi());
	}
}


c.java代码获取serviceProxy

package com.tangjun.ws.cxf.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.tangjun.ws.cxf.service.interfaces.HelloWorld;

public class JaxWsProxyFactoryTest {
	public static void main(String[] args) {
		JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
		
		fac.setServiceClass(HelloWorld.class);
		fac.setAddress("http://localhost:9090/exmaple_cxf_server/services/helloWorld");
		
		HelloWorld service = (HelloWorld) fac.create();
		System.out.println(service.sayHi());
	}
}


第三种方法和第二种方法相比,不需要配置spring文件,缺点也很明显,代码重用会非常难。第二种和第一种方式比起来,优点是可以重写factorybean,缺点是不好做拦截器配置。所以常规还是推荐第一种。

3.测试结果

>helloworld, Wed Apr 22 16:20:54 CST 2015

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值