即在在运行期间在内存中动态的生成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
分享到:
相关推荐
CXF webService 工具类
CXF webservice,mybatis持久层框架,增删改查实例;完整jar包;
cxf WebService jar包
Web Service学习-CXF开发Web Service的权限控制(二)
maven项目 cxf webservice mongodb mysql 公司真实现目初期实现
实战Web+Service+with+CXF webservice快速入门 webservice快速入门
mybatis+spring+cxf webservice服务 项目框架代码例子,绝对是可以参考的,信不信,只有下载之后才知道
使用jdk1.6、cxf2.3和tomcat开发的一个关于验证码的webservice接口,主要实现对手机验证码的验证。
CXF WebService整合Spring代码(包含服务,客户端两个工程 和 文档) 需要视频的话,留邮箱
使用 CXF 做 webservice 简单例子,用的是cxf-2.1.3.jar
使用cxf webservice时容易出现的异常
SSH整合cxf webservice实例
一、CXF WebService准备工作 二、CXF 入门示例 三、CXF对Interceptor拦截器的支持 四、CXF WebService中传递复杂类型对象 五、CXF整合Spring
现在主流的webService框架,一个是axis2,一个是cxf,本资源是基于cxf框架的简单例子
cxf WebService jar包
cxf webService
CXF 一共包含5个项目,已经把客服端/服务器分开,所有的jar都在最后一个项目的lib里,直接cope到其他四个里就行了,1个项目传string,有两个项目传对象,还有两个与spring集成并传对象
webservice示例 springmvc+maven+cxf发布webservice 博客地址 http://blog.csdn.net/joe_storm/article/details/78839150
JAVA CXF webservice的示例代码 Eclipse工程
2.用cxf开发webservice 3.这个服务端和客户端的小demo 在服务端 对外开放接口服务,然后在客户端 调用服务端的方法, 实现客户端(一个javaweb项目)对服务端(javaweb项目)方法的调用, 实际上就是发送和接收消息...