即在在运行期间在内存中动态的生成Client(SEI和Data Class). CXF提供了JaxWsDynamicClientFactory用于创建
Dynamic Client.
完整代码参考附件:webservice-sample.zip
1. 创建一个简单的Dynamic Client
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.apache.cxf.endpoint.Client; import java.lang.reflect.Method; public class OrderProcessJaxWsDynamicClient { public OrderProcessJaxWsDynamicClient() { } public static void main(String str[]) throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8080/OrderProcess?wsdl"); Object order = Thread.currentThread().getContextClassLoader().loadClass("org.pcdp.sample.webservice.server.Order").newInstance(); Method m1 = order.getClass().getMethod("setCustomerID", String.class); Method m2 = order.getClass().getMethod("setItemID", String.class); Method m3 = order.getClass().getMethod("setQty", Integer.class); Method m4 = order.getClass().getMethod("setPrice", Double.class); m1.invoke(order, "C001"); m2.invoke(order, "I001"); m3.invoke(order, 100); m4.invoke(order, 200.00); Object[] response = client.invoke("processOrder", order); System.out.println("Response is " + response[0]); } }
运行输出:
信息: Created classes: org.pcdp.sample.webservice.server.ObjectFactory, org.pcdp.sample.webservice.server.Order, org.pcdp.sample.webservice.server.ProcessOrder, org.pcdp.sample.webservice.server.ProcessOrderResponse Response is ORD1234
2. Using the CXF service model for building dynamic client
import java.beans.PropertyDescriptor; 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; public class OrderProcessJaxWsDynClient { public OrderProcessJaxWsDynClient() { } public static void main(String str[]) throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8080/OrderProcess?wsdl"); Endpoint endpoint = client.getEndpoint(); // Make use of CXF service model to introspect the existing WSDL ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0); QName bindingName = new QName("http://server.webservice.sample.pcdp.org/","OrderProcessServiceSoapBinding"); BindingInfo binding = serviceInfo.getBinding(bindingName); QName opName = new QName("http://server.webservice.sample.pcdp.org/", "processOrder"); BindingOperationInfo boi = binding.getOperation(opName); // Operation name is processOrder BindingMessageInfo inputMessageInfo = null; if (!boi.isUnwrapped()) { // OrderProcess uses document literal wrapped style. inputMessageInfo = boi.getWrappedOperation().getInput(); } else { inputMessageInfo = boi.getUnwrappedOperation().getInput(); } List<MessagePartInfo> parts = inputMessageInfo.getMessageParts(); MessagePartInfo partInfo = parts.get(0); // Input class is Order // Get the input class Order Class<?> orderClass = partInfo.getTypeClass(); Object orderObject = orderClass.newInstance(); // Populate the Order bean // Set customer ID, item ID, price and quantity PropertyDescriptor custProperty = new PropertyDescriptor("customerID",orderClass); custProperty.getWriteMethod().invoke(orderObject, "C001"); PropertyDescriptor itemProperty = new PropertyDescriptor("itemID",orderClass); itemProperty.getWriteMethod().invoke(orderObject, "I001"); PropertyDescriptor priceProperty = new PropertyDescriptor("price",orderClass); priceProperty.getWriteMethod().invoke(orderObject,Double.valueOf(100.00)); PropertyDescriptor qtyProperty = new PropertyDescriptor("qty",orderClass); qtyProperty.getWriteMethod().invoke(orderObject, Integer.valueOf(20)); // Invoke the processOrder() method and print the result // The response class is String Object[] result = client.invoke(opName, orderObject); System.out.println("The order ID is " + result[0]); } }
运行输出:
信息: Created classes: org.pcdp.sample.webservice.server.ObjectFactory, org.pcdp.sample.webservice.server.Order, org.pcdp.sample.webservice.server.ProcessOrder, org.pcdp.sample.webservice.server.ProcessOrderResponse The order ID is ORD1234