CXF + WebService

There are many ways for cxf to develop webservice, the common ones are as follows:

   1. Proxy method: JaxWsProxyFactoryBean;
   2. Dynamic client: JaxWsDynamicClientFactory;
   3. Automatic command generation: wsdl2java -p cn.creditease.orgams.test.cxf -dd:\cxf \src -all http://10.106.91.47:8080/jeesxb-web-orgams-api/services/creditWS?wsdl

make a simple comparison of the first two methods (only personal understanding, not authoritative, welcome to add bricks)  

   JaxWsProxyFactoryBean
   Introduction: The calling method adopts a mechanism similar to RMI, that is, the client directly provides the service interface (interface) provided by the server, CXF generates the proxy object of the remote service through the runtime proxy, and completes the access to the webservice on the client; several must be Filled field: setAddress-this is the address when we publish the webservice, keep it consistent
     Disadvantage: The advantage of this kind of calling service is that the calling process is very simple, a webservice call is completed with just a few lines of code, but the client must also rely on the server-side Interface, this invocation method is very restrictive, and requires that the webservice on the server side must be implemented in java--this also loses the meaning of using webservice.

  Introduction to JaxWsDynamicClientFactory
     : As long as you specify the location of the server-side wsdl file, and then specify the method to be called and the parameters of the method, you don't care about the implementation of the server-side.

Without further ado, let's post the tools of the dynamic client first, as follows:


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 client tool class
 * @Author: Terry
 * @Company: **
 * @Version: V1.0
 * @Create: Aug 8, 2016 at 3:18:03 PM
 */
public class HttpClientWS {

	/*
	 * Client call
	 */
	public static Object[] webServiceClient(String webServiceUrl, String server, String targetNameSpace,
			String soapBinding, HashMap<String, Object> map) {

		// dynamic client
		JaxWsDynamicClientFactory jwdcf = JaxWsDynamicClientFactory.newInstance();
		// call the service
		Client client = jwdcf.createClient(webServiceUrl);
                 //Set the timeout unit to milliseconds  
                 HTTPConduit http = (HTTPConduit) client.getConduit();        
                 HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();        
                 httpClientPolicy.setConnectionTimeout(3000); //Connection timeout      
                 httpClientPolicy.setAllowChunking(false); //Cancel chunk encoding   
                 httpClientPolicy.setReceiveTimeout(3000); //Response timeout  
                 http.setClient(httpClientPolicy);
		Object[] obj = null;
		try {
			obj = client.invoke(server, getObject(client, server, targetNameSpace, soapBinding, map));

		} catch (Exception e) {
			e.printStackTrace ();
		}
		return obj;
	}

	/*
	 * assembled parameters
	 */
	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);
		// Create a QName to specify the NameSpace and the service to call
		QName bindingName = new QName(targetNameSpace, soapBinding);
		BindingInfo binding = serviceInfo.getBinding(bindingName);
		// Create a QName to specify the NameSpace and the method to call
		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);

	}

	/*
	 * Assembly parameter properties
	 */
	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();
		// get object instance
		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); // Get class attributes
		Object obj = type.newInstance(); // Create JavaBean object

		// Assign values ​​to properties of the JavaBean object
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (int i = 0; i < propertyDescriptors.length; i++) {
			PropertyDescriptor descriptor = propertyDescriptors[i];
			String propertyName = descriptor.getName();

			if (map.containsKey(propertyName)) {
				// The following sentence can be tried, so that when a property assignment fails, it will not affect other property assignments.
				Object value = map.get(propertyName);

				Object[] args = new Object[1];
				args[0] = value;

				descriptor.getWriteMethod().invoke(obj, args);
			}
		}
		return obj;
	}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037482&siteId=291194637