Apache CXF基本使用
一、服务端开发
1、创建web项目
2、导入jar包
3、web.xml中配置Servlet
1 <!-- 配置CXF框架提供的Servlet --> 2 <servlet> 3 <servlet-name>cxf</servlet-name> 4 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 5 <!-- 通过初始化参数指定CXF框架的配置文件位置 --> 6 <init-param> 7 <param-name>config-location</param-name> 8 <param-value>classpath:cxf.xml</param-value> 9 </init-param> 10 </servlet> 11 <servlet-mapping> 12 <servlet-name>cxf</servlet-name> 13 <url-pattern>/service/*</url-pattern> 14 </servlet-mapping
4、在类路径下提供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" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 引入CXF Bean定义如下,早期的版本中使用 --> <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" /> </beans>
5、开发一个接口和实现类
1 @WebService 2 public interface CustomerService { 3 4 public List<Customer> findAll(); 5 //查询未关联的 6 public List<Customer> findListNoAssociation(); 7 //查询已经关联的 8 public List<Customer> findListHasAssociation(String id); 9 //定区关联客户 10 public void assigncustomerstodecidedzone(String decidedzoneId,Integer[] customerIds); 11 //根据客户的手机号查询客户信息 12 public Customer findByTelephone(String telephone); 13 //根据客户地址查询定区ID 14 public String findDecidedzoneIdByAddress(String address); 15 }
6、在cxf.xml中注册服务
1 <!-- 基于接口形式发布的服务 --> 2 <bean id="customerService" class="com.itheima.crm.service.impl.CustomerServiceImpl"> 3 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 4 </bean> 5 <jaxws:server id="myService" address="/customer"> 6 <jaxws:serviceBean> 7 <ref bean="customerService"/> 8 </jaxws:serviceBean> 9 </jaxws:server>
二、客户端开发
1、使用wsimport或者CXF提供wsdl2java生成本地代码,只需要生成接口文件
2、将bean及接口文件复制到项目中
3、提供spring配置文件,注册客户端代理对象
1 <jaxws:client id="customerService" serviceClass="com.itheima.crm.CustomerService" address="http://localhost:8080/crm/service/customer"></jaxws:client>