Apache CXF基本使用
阿新 • • 發佈:2018-12-02
一、服務端開發
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>