1、代理方式:JaxWsProxyFactoryBean;
2、动态客户端:JaxWsDynamicClientFactory;
3、命令自动生成:wsdl2java -p cn.creditease.orgams.test.cxf -d d:\cxf\src -all http://10.106.91.47:8080/jeesxb-web-orgams-api/services/creditWS?wsdl
针对前两张方式简单做下对比(仅仅个人理解,不权威,欢迎拍砖补充)
JaxWsProxyFactoryBean
简介:调用方式采用了和RMI类似的机制,即客户端直接服务器端提供的服务接口(interface),CXF通过运行时代理生成远程服务的代理对象,在客户端完成对webservice的访问;几个必填的字段:setAddress-这个就是我们发布webservice时候的地址,保持一致
缺点:这种调用service的好处在于调用过程非常简单,就几行代码就完成一个webservice的调用,但是客户端也必须依赖服务器端的接口,这种调用方式限制是很大的,要求服务器端的webservice必须是java实现--这样也就失去了使用webservice的意义。
JaxWsDynamicClientFactory
简介:只要指定服务器端wsdl文件的位置,然后指定要调用的方法和方法的参数即可,不关心服务端的实现方式。
闲话少说,先把动态客户端的工具类贴出来,如下:
import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.apache.cxf.service.model.BindingInfo; import org.apache.cxf.service.model.BindingMessageInfo; import org.apache.cxf.service.model.BindingOperationInfo; import org.apache.cxf.service.model.MessagePartInfo; import org.apache.cxf.service.model.ServiceInfo; import org.apache.cxf.transport.http.HTTPConduit; import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import java.beans.Introspector; /** * @Description: webservice客户端工具类 * @Author: Terry * @Company: ** * @Version: V1.0 * @Create : 2016年8月8日 下午3:18:03 */ public class HttpClientWS { /* * 客户端调用 */ public static Object[] webServiceClient(String webServiceUrl, String server, String targetNameSpace, String soapBinding, HashMap<String, Object> map) { // 动态客户端 JaxWsDynamicClientFactory jwdcf = JaxWsDynamicClientFactory.newInstance(); // 调用服务 Client client = jwdcf.createClient(webServiceUrl); //设置超时单位为毫秒 HTTPConduit http = (HTTPConduit) client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); httpClientPolicy.setConnectionTimeout(3000); //连接超时 httpClientPolicy.setAllowChunking(false); //取消块编码 httpClientPolicy.setReceiveTimeout(3000); //响应超时 http.setClient(httpClientPolicy); Object[] obj = null; try { obj = client.invoke(server, getObject(client, server, targetNameSpace, soapBinding, map)); } catch (Exception e) { e.printStackTrace(); } return obj; } /* * 拼装入参 */ public static Object getObject(Client client, String server, String targetNameSpace, String soapBinding, HashMap<String, Object> map) throws InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException { Endpoint endpoint = client.getEndpoint(); ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0); // 创建QName来指定NameSpace和要调用的service QName bindingName = new QName(targetNameSpace, soapBinding); BindingInfo binding = serviceInfo.getBinding(bindingName); // 创建QName来指定NameSpace和要调用的方法 QName opName = new QName(targetNameSpace, server); BindingOperationInfo boi = binding.getOperation(opName); BindingMessageInfo inputMessageInfo = null; if (!boi.isUnwrapped()) { inputMessageInfo = boi.getWrappedOperation().getInput(); } else { inputMessageInfo = boi.getUnwrappedOperation().getInput(); } return setInputProperty(opName, binding, inputMessageInfo, map); } /* * 拼装入参属性 */ public static Object setInputProperty(QName opName, BindingInfo binding, BindingMessageInfo inputMessageInfo, HashMap<String, Object> map) throws InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException { List<MessagePartInfo> parts = inputMessageInfo.getMessageParts(); // 取得对象实例 MessagePartInfo partInfo = parts.get(0); Class<?> partClass = partInfo.getTypeClass(); Object inputObject = partClass.newInstance(); for (String key : map.keySet()) { PropertyDescriptor partPropertyDescriptor = new PropertyDescriptor(key, partClass); Method setBodysigninfo = partPropertyDescriptor.getWriteMethod(); setBodysigninfo.invoke(inputObject, map.get(key)); } return inputObject; } /* * javabean转map */ public static HashMap convertBeanToMap(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException { Class type = bean.getClass(); HashMap returnMap = new HashMap(); BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (!propertyName.equals("class")) { Method readMethod = descriptor.getReadMethod(); Object result = readMethod.invoke(bean, new Object[0]); if (result != null) { returnMap.put(propertyName, result); } else { returnMap.put(propertyName, ""); } } } return returnMap; } /* * map转javabean */ public static Object convertMapToBean(Class type, HashMap map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性 Object obj = type.newInstance(); // 创建 JavaBean 对象 // 给 JavaBean 对象的属性赋值 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) { // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。 Object value = map.get(propertyName); Object[] args = new Object[1]; args[0] = value; descriptor.getWriteMethod().invoke(obj, args); } } return obj; } }