一、创建Server
1.0创建PO
public class Order {
/**
* 订单编号
*/
private String orderId;
/**
* 订单日期
*/
private Date orderDate;
/**
* 下单人ID
*/
private String userId;
/**
* 订单状态
*/
private byte orderStatus;
/**
* @return the orderId
*/
public String getOrderId() {
return orderId;
}
/**
* @param orderId the orderId to set
*/
public void setOrderId(String orderId) {
this.orderId = orderId;
}
/**
* @return the orderDate
*/
public Date getOrderDate() {
return orderDate;
}
/**
* @param orderDate the orderDate to set
*/
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
/**
* @return the userId
*/
public String getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
* @return the orderStatus
*/
public byte getOrderStatus() {
return orderStatus;
}
/**
* @param orderStatus the orderStatus to set
*/
public void setOrderStatus(byte orderStatus) {
this.orderStatus = orderStatus;
}
}
1.1创建WebService接口
@WebService
public interface OrderService{
Order getOrder(String orderId);
}
1.2创建接口实现类
@WebService(endpointInterface="cn.com.demo.order.service.OrderService",serviceName="orderProcessService")
public class OrderServiceImpl implements OrderService {
public Order getOrder(String orderId) {
Order order=new Order();
Date d=new Date();
order.setOrderId("111");
byte s=0;
order.setOrderStatus(s);
order.setOrderDate(d);
order.setUserId("001");
return order;
}
}
1.3创建applicationContext-cxf.xml配置文件,文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Load CXF modules from cxf.jar -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="order"
implementor="cn.com.demo.order.service.impl.OrderServiceImpl"
address="/cxfOrder"/>
</beans>
二、在web.xml中配置CXFServelt和Spring监听器,内容如下
加载Spring配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-cxf.xml</param-value>
</context-param>
配置Spring监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置CXFServlet
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
三、创建Client
3.0创建客户端WebService,Service的包名和接口名可以与Server端不一样(包的名称最好与服务端一致,防止不能访问),接口中的方法必须与服务端一致
@WebService
public interface OrderProcess {
Order getOrder(String orderId);
}
3.1创建PO,包名可与Server端不相同,类名和属性必须与Server端一致
public class Order {
/**
* 订单编号
*/
private String orderId;
/**
* 订单日期
*/
private Date orderDate;
/**
* 下单人ID
*/
private String userId;
/**
* 订单状态
*/
private byte orderStatus;
/**
* @return the orderId
*/
public String getOrderId() {
return orderId;
}
/**
* @param orderId the orderId to set
*/
public void setOrderId(String orderId) {
this.orderId = orderId;
}
/**
* @return the orderDate
*/
public Date getOrderDate() {
return orderDate;
}
/**
* @param orderDate the orderDate to set
*/
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
/**
* @return the userId
*/
public String getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
* @return the orderStatus
*/
public byte getOrderStatus() {
return orderStatus;
}
/**
* @param orderStatus the orderStatus to set
*/
public void setOrderStatus(byte orderStatus) {
this.orderStatus = orderStatus;
}
}
3.2创建Client
public class OrderClient{
private static JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();
public static Object getBean(String url,Class<?> serviceClass){
factory.setServiceClass(serviceClass);
factory.setAddress(url);
OrderProcess orderProcess=(OrderProcess)factory.create();
return orderProcess;
}
}
创建测试类,测试是否能调用
public class Test {
public static void main(String args[]){
OrderProcess orderService=(OrderProcess)OrderClient.getBean("http://localhost:8080/orderdemo/services/cxfOrder", OrderProcess.class);
System.out.println("====================================== "+orderService.getOrder("12").getOrderId());
}
}
3.3上一步创建OrderClient的代码可通过Spring配置实现,创建applicationContext_cxfclient.xml,配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- cxf webservice client -->
<!-- client service interface -->
<bean id="client" class="cn.com.cxf.demo.order.service.OrderProcess" factory-bean="clientFactory" factory-method="create" />
<!-- apache cxf jaxwsProxyFactorybean -->
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
<!-- service class -->
<property name="serviceClass" value="cn.com.cxf.demo.order.service.OrderProcess" />
<!-- service address -->
<property name="address" value="http://localhost:8080/orderdemo/services/cxfOrder" />
</bean>
</beans>
3.4创建测试类
public class OrderTest{
public static void main(String args[]) throws Exception {
//读取spring 配置文件
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext(new String[] {"spring/applicationContext_cxfclient.xml"});
//获取客户端的Service接口
OrderProcess clientService=(OrderProcess) context.getBean("client");
System.out.println("===============getOrder:"+clientService.getOrder("11").getOrderId());
}
}