Apache CXF 调用出现的问题

首先申明我用 apache CXF 版本是 apache-cxf-2.5.10.

原文引用:http://blog.csdn.net/crazycoder2010/article/details/6673982

OSChina转载:http://www.oschina.net/question/54100_26065

今天突然想试试之前一直没有心情尝试的Apache CXF,就在OSChina上找示例,结果发现一篇很好的文章. 见上.

我采用他写的代码,WebService的Server段基本都搭好,我在Clinet段调用,出现问题.

package cn.haohaowo.webservice.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsClientFactoryBean;

import cn.haohaowo.webservice.HelloWorldService;

public class WebServiceClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		JaxWsClientFactoryBean jaxWsClientFactoryBean = new JaxWsClientFactoryBean();
		jaxWsClientFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
		jaxWsClientFactoryBean.getInFaultInterceptors().add(new LoggingOutInterceptor());
		jaxWsClientFactoryBean.setServiceClass(HelloWorldService.class);
		jaxWsClientFactoryBean.setAddress("http://localhost:9000/helloWorldService");
		HelloWorldService helloWorldService = (HelloWorldService) jaxWsClientFactoryBean.create();
		String result = helloWorldService.sayHello("Robinson");
		System.out.println(result);
		
//		Client client = jaxWsClientFactoryBean.create();
//		Object[] obj = client.invoke("sayHello", "Robinson");
//		System.out.println(obj[0]);
	}

}

2013-5-2 14:52:33 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.haohaowo.cn/}HelloWorldServiceService from class cn.haohaowo.webservice.HelloWorldService
Exception in thread "main" java.lang.ClassCastException: org.apache.cxf.endpoint.ClientImpl cannot be cast to cn.haohaowo.webservice.HelloWorldService
	at cn.haohaowo.webservice.client.WebServiceClient.main(WebServiceClient.java:21)
大家可以看到,很明显是一个类型转换错误.当时我就纳闷,为什么是这个错误呢.

但是当我看到另外一个实例代码,和其他语言交互的时候官方写法,我才发觉给出示例的人把代码写错了.

jaxWsClientFactoryBean.create()
通过字面的意思就知道,这个客户端仓库类创建一个实例,应该实例化的Client类,而不是Service类. 谨记呀... 

你可能感兴趣的:(apache,webservice,CXF)