CXF提供了很多client端的调用方法。这里让你快速了解这些选择,帮助你判断使用哪种。
参见:
http://cxf.apache.org/docs/how-do-i-develop-a-client.html
源代码主体使用PacktPub.Apache.CXF.Web.Service.Development.Dec.2009书中的代码。本例中,将5种调用方式集中在一个工程中,并使用Maven重组的调用方式。
本例下载地址:
http://dl.iteye.com/topics/download/c647dae6-de86-3eec-9590-7fcf83e9def4
-
WSDL2Java generated Client
-
JAX-WS Proxy
-
JAX-WS Dispatch APIs
-
JAX-WS Client Proxy
-
Dynamic Client
-
Reflection API
-
Service Model API
-
在这里Dispatch本节内容不做介绍。
WSDL2Java generated Client
也就是依据wsdl文件生成java客户端,直接调用。查看调用方式,也就是OrderProcessService的getOrderProcessPort方法,获得服务的引用。
启动服务端
mvn test –Pserver
在启动服务端时,通过maven-compiler-plugin编译class,通过cxf-codegen-plugin依据src/main/resources/OrderProcess.wsdl生成存根类OrderProcessService
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.cxf</groupId>
- <artifactId>cxf-codegen-plugin</artifactId>
- <version>${cxf.version}</version>
- <executions>
- <execution>
- <id>generate-sources</id>
- <phase>generate-sources</phase>
- <configuration>
- <wsdlOptions>
- <wsdlOption>
- <wsdl>src/main/resources/OrderProcess.wsdl</wsdl>
- </wsdlOption>
- </wsdlOptions>
- </configuration>
- <goals>
- <goal>wsdl2java</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
启动客户端
mvn test -PStubClient
JAX-WS Proxy
和生成存根类的方式相反,proxy是不需要执行wsdl2java的。但是在编译环境中需要接口类和VO类。这里,通过指定WSDL_LOCATION和PORT_NAME,使用Service.create创建service,使用service.getPort获得服务引用。
- package demo.order.client;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- import demo.order.Order;
- import demo.order.OrderProcess;
- public class ProxyClient {
- private static final QName SERVICE_NAME = new QName(“http://order.demo/”, “OrderProcessService”);
- private static final QName PORT_NAME = new QName(“http://order.demo/”, “OrderProcessPort”);
- private static final String WSDL_LOCATION = “http://localhost:8080/OrderProcess?wsdl”;
- public static void main(String args[]) throws Exception {
- URL wsdlURL = new URL(WSDL_LOCATION);
- Service service = Service.create(wsdlURL, SERVICE_NAME);
- OrderProcess port = service.getPort(PORT_NAME, OrderProcess.class);
- Order order = new Order();
- order.setCustomerID(“C001”);
- order.setItemID(“I001”);
- order.setPrice(100.00);
- order.setQty(20);
- String result = port.processOrder(order);
- System.out.println(“The order ID is “ + result);
- }
- }
启动服务端
mvn test –Pserver
启动客户端
mvn test -PProxyClient
Client Proxy
使用JaxWsProxyFactoryBean 类简化Proxy。
- package demo.order.client;
- import demo.order.Order;
- import demo.order.OrderProcess;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- public class ClientProxyClient {
- public static void main(String args[]) throws Exception {
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- factory.setServiceClass(OrderProcess.class);
- factory.setAddress(“http://localhost:8080/OrderProcess”);
- OrderProcess service = (OrderProcess)factory.create();
- Order order = new Order();
- order.setCustomerID(“C001”);
- order.setItemID(“I001”);
- order.setPrice(100.00);
- order.setQty(20);
- String result = service.processOrder(order);
- System.out.println(“The order ID is “ + result);
- }
- }
启动服务端
mvn test –Pserver
启动客户端
mvn test -PClientProxyClient
Dynamic Client
甚至不需要SEI接口类,
Reflection API
JaxWsDynamicClientFactory.newInstance获得JaxWsDynamicClientFactory实例。通过dcf.createClient获得Client客户端引用。
- package demo.order.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(“demo.order.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]);
- }
- }
启动服务端
mvn test –Pserver
启动客户端
mvn test -POrderProcessJaxWsDynamicClient
Service Model API
最后,Service Model是CXF内置的获取Service的信息。
- package demo.order.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://order.demo/”, “OrderProcessServiceSoapBinding”);
- BindingInfo binding = serviceInfo.getBinding(bindingName);
- QName opName = new QName(“http://order.demo/”, “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]);
- }
- }
启动服务端
mvn test –Pserver
启动客户端
mvn test -POrderProcessJaxWsDynClient
今天的文章eclipse如何编译java项目_app客户端怎么开发分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/59131.html