cxf 用JaxWsDynamicClientFactory 怎么传递参数对象 cxfwebservice对象 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 问题已经解决,不容易呀,主要是对CXF不熟悉,通过查询CXF文档和apache-cxf-2.6.14\samples\wsdl_first_dynamic_client这个DEMO,最终把问题解决了,在此分享一下,不多说直接贴代码,其中有些敏感信息,不方便贴出,忘请见谅,希望对遇到同类问题的朋友有点用处。public class GetWebServiceData { private static final QName SERVICE_NAME = new QName(namespace, serviceName); /** * * @throws Exception */ public static void main(String[] args) throws Exception { // 远程webService的URL String hostUrl = "****************?wsdl"; try { // 创建动态客户端 JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); // 创建客户端连接 Client client = factory.createClient(hostUrl, SERVICE_NAME); ClientImpl clientImpl = (ClientImpl) client; Endpoint endpoint = clientImpl.getEndpoint(); // Make use of CXF service model to introspect the existing WSDL ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0); // 创建QName来指定NameSpace和要调用的service QName bindingName = new QName(namespace, service); BindingInfo binding = serviceInfo.getBinding(bindingName); // 创建QName来指定NameSpace和要调用的方法 QName opName = new QName(namespace, methodName); BindingOperationInfo boi = binding.getOperation(opName); BindingMessageInfo inputMessageInfo = boi.getInput(); List<MessagePartInfo> parts = inputMessageInfo.getMessageParts(); // 取得对象实例 MessagePartInfo partInfo = parts.get(0); Class<?> partClass = partInfo.getTypeClass(); Object inputObject = partClass.newInstance(); // 取得字段的set方法并赋值 PropertyDescriptor partPropertyDescriptor = new PropertyDescriptor(字段名, partClass); Method userNoSetter = partPropertyDescriptor.getWriteMethod(); userNoSetter.invoke(inputObject, 属性值); // 取得字段的set方法并赋值 PropertyDescriptor partPropertyDescriptor2 = new PropertyDescriptor(字段名, partClass); Method productCodeSetter = partPropertyDescriptor2.getWriteMethod(); productCodeSetter.invoke(inputObject, 属性值); // 调用客户端invoke()方法,把inputObject传递给要调用的方法并取得结果对象 Object[] result = client.invoke(opName, inputObject); // 取得的结果是一个对象 Class<?> resultClass = result[0].getClass(); // 取得返回结果的get方法并得到它的值 PropertyDescriptor resultDescriptor = new PropertyDescriptor(结果字段名, resultClass); Object resultGetter = resultDescriptor.getReadMethod().invoke(result[0]); System.out.println("result:" + resultGetter); // 取得返回结果的get方法并得到它的值 PropertyDescriptor tokenDescriptor = new PropertyDescriptor(结果字段名, resultClass); // 取得的是一个对象实例 Object getObj= tokenDescriptor.getReadMethod().invoke(result[0]); if(tokenGetter != null) { Class<?> resultTokenClass = tokenDescriptor.getReadMethod().invoke(result[0]).getClass(); // 得到对象实例下的***属性值 PropertyDescriptor expiredTimeDescriptor = new PropertyDescriptor(字段名, resultTokenClass); Object getter = expiredTimeDescriptor.getReadMethod().invoke(getObj); System.out.println("字段名:" + getter ); } } catch (Exception e) { e.printStackTrace(); } } 看一下这个。http://cxf.apache.org/docs/dynamic-clients.html要简单很多。 hibernate 3.2 往mysql中写入中文出现乱码问题 struts2 token问题 DBF格式文件下载路径怎么自定义(在线求助!!!) MyEclipse下ssh-jar包的冲突问题…… 使用struts报错机制,出现了问题 大家指教 resin问题 正则问题, 求大神来 javamail中有关于ssl的东西吗? 高分求struts分页代码,符合mvc的,急(100) ejb好像都是在写会话bean,实体bean怎么写? 【新人求助】执行maven生成的jar文件要不要修改classpath啊 用的c3p0的连接池,这个问题是哪个jar包和哪个冲突了啊
private static final QName SERVICE_NAME = new QName(namespace, serviceName);
/**
*
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 远程webService的URL
String hostUrl = "****************?wsdl";
try {
// 创建动态客户端
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
// 创建客户端连接
Client client = factory.createClient(hostUrl, SERVICE_NAME);
ClientImpl clientImpl = (ClientImpl) client;
Endpoint endpoint = clientImpl.getEndpoint();
// Make use of CXF service model to introspect the existing WSDL
ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0);
// 创建QName来指定NameSpace和要调用的service
QName bindingName = new QName(namespace, service);
BindingInfo binding = serviceInfo.getBinding(bindingName);
// 创建QName来指定NameSpace和要调用的方法
QName opName = new QName(namespace, methodName);
BindingOperationInfo boi = binding.getOperation(opName);
BindingMessageInfo inputMessageInfo = boi.getInput();
List<MessagePartInfo> parts = inputMessageInfo.getMessageParts();
// 取得对象实例
MessagePartInfo partInfo = parts.get(0);
Class<?> partClass = partInfo.getTypeClass();
Object inputObject = partClass.newInstance(); // 取得字段的set方法并赋值
PropertyDescriptor partPropertyDescriptor = new PropertyDescriptor(字段名, partClass);
Method userNoSetter = partPropertyDescriptor.getWriteMethod();
userNoSetter.invoke(inputObject, 属性值); // 取得字段的set方法并赋值
PropertyDescriptor partPropertyDescriptor2 = new PropertyDescriptor(字段名, partClass);
Method productCodeSetter = partPropertyDescriptor2.getWriteMethod();
productCodeSetter.invoke(inputObject, 属性值); // 调用客户端invoke()方法,把inputObject传递给要调用的方法并取得结果对象
Object[] result = client.invoke(opName, inputObject);
// 取得的结果是一个对象
Class<?> resultClass = result[0].getClass();
// 取得返回结果的get方法并得到它的值
PropertyDescriptor resultDescriptor = new PropertyDescriptor(结果字段名, resultClass);
Object resultGetter = resultDescriptor.getReadMethod().invoke(result[0]);
System.out.println("result:" + resultGetter);
// 取得返回结果的get方法并得到它的值
PropertyDescriptor tokenDescriptor = new PropertyDescriptor(结果字段名, resultClass);
// 取得的是一个对象实例
Object getObj= tokenDescriptor.getReadMethod().invoke(result[0]);
if(tokenGetter != null) {
Class<?> resultTokenClass = tokenDescriptor.getReadMethod().invoke(result[0]).getClass();
// 得到对象实例下的***属性值
PropertyDescriptor expiredTimeDescriptor = new PropertyDescriptor(字段名, resultTokenClass);
Object getter = expiredTimeDescriptor.getReadMethod().invoke(getObj);
System.out.println("字段名:" + getter );
}
} catch (Exception e) {
e.printStackTrace();
}
}
http://cxf.apache.org/docs/dynamic-clients.html
要简单很多。