采用Simple frontend方式开发WebService方式主要是使用Simple Factory组件基于反射的概念去构建和发布服务,不
需要在接口和实现类上添加注解。
完整代码参考http://springsfeng.iteye.com/blog/1634753附件。
1. 创建接口和实现类
package org.pcdp.sample.simplefrontend; public interface OrderProcess { String processOrder(Order order); }
package org.pcdp.sample.simplefrontend; public class OrderProcessImpl implements OrderProcess { public String processOrder(Order order) { System.out.println("Processing order..."); String orderID = validate(order); return orderID; } /** * Validates the order and returns the order ID **/ private String validate(Order order) { String custID = order.getCustomerID(); String itemID = order.getItemID(); int qty = order.getQty(); double price = order.getPrice(); if (custID != null && itemID != null && !custID.equals("") && !itemID.equals("") && qty > 0 && price > 0.0) { return "ORD1234"; } return null; } }
2. 创建Server端实现
import org.apache.cxf.frontend.ServerFactoryBean; public class Server { public static void main(String[] arg) { // Create service implementation OrderProcessImpl orderProcessImpl = new OrderProcessImpl(); // Create Server ServerFactoryBean svrFactory = new ServerFactoryBean(); svrFactory.setServiceClass(OrderProcess.class); svrFactory.setAddress("http://localhost:8080/SimpleOrderProcess"); svrFactory.setServiceBean(orderProcessImpl); svrFactory.create(); } }
3. 创建Client端实现
import org.apache.cxf.frontend.ClientProxyFactoryBean; import org.pcdp.sample.simplefrontend.Order; import org.pcdp.sample.simplefrontend.OrderProcess; public class SimpleClient { public static void main(String[] args) { ClientProxyFactoryBean factory = new ClientProxyFactoryBean(); factory.setServiceClass(OrderProcess.class); factory.setAddress("http://localhost:8080/SimpleOrderProcess"); OrderProcess client = (OrderProcess) factory.create(); Order order = new Order(); order.setCustomerID("C001"); order.setItemID("I001"); order.setPrice(100.00); order.setQty(20); String result = client.processOrder(order); System.out.println("The order ID is " + result); } }