webService之(四)CXF客户端

CXF提供了很多client端的调用方法。官方提供的文档可以让你快速了解这些选择,帮助你判断使用哪种。
参考地址:http://cxf.apache.org/docs/index.html
本文将介绍两种方式:

首先本项目使用mavent管理,通过cxf-codegen-plugin生成客户端代码

<properties>
	<cxf.version>3.0.3</cxf.version>
</properties>
<build>
	<plugins>
		<plugin>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-codegen-plugin</artifactId>
			<version>${cxf.version}</version>
			<executions>
				<execution>
					<id>generate-sources</id>
					<phase>generate-sources</phase>
					<configuration>
						<sourceRoot>${project.build.directory}/services</sourceRoot>
						<wsdlOptions>
							<wsdlOption>
								<wsdl>http://localhost:8889/ms?wsdl</wsdl>
								<extraargs>
									<extraarg>-impl</extraarg>
									<extraarg>-verbose</extraarg>
								</extraargs>
							</wsdlOption>
						</wsdlOptions>
					</configuration>
					<goals>
						<goal>wsdl2java</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

1、Client Proxy
使用JaxWsProxyFactoryBean类简化Proxy。

public static void main(String[] args) {
	//使用CXF的方式访问
	JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
	fac.setServiceClass(IPersonService.class);
	fac.setAddress("http://localhost:8889/ms");
	IPersonService ms = (IPersonService) fac.create();
	Person person = ms.getPersonInfos();
	
	System.out.println("name:" + person.getName() + ",sex:" + person.getSex() + ",email:" + person.getEmail());
}

2、Dynamic Client 
不需要SEI接口类,JaxWsDynamicClientFactory.newInstance获得JaxWsDynamicClientFactory实例,
通过dcf.createClient获得Client客户端引用。

@Test
public void test002() throws Exception{
	
	JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
	Client client = dcf.createClient("http://localhost:8889/ms?wsdl");
	
	Object[] objects = client.invoke("getPersonInfos");
	
	Person person = (Person) objects[0];
	
	System.out.println("name:" + person.getName() + ",sex:" + person.getSex() + ",email:" + person.getEmail());
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值