[CXF]WebService框架CXF

**

WebService-CXF

**
什麼是CXF
Apache CXF = Celtix + Xfire
支持多種協議:
SOAP1.1,1.2
XML/HTTP
CORBA(Common Object Request Broker Architecture公共對象請求代理體系結構,早期語言使用的WS。C,c++,C#)

並可以與Spring進行快速無縫的整合
靈活的部署:可以運行在Tomcat,Jboss,Jetty(內置),IBMWS,BeaWL上面。
在這裏插入圖片描述
在這裏插入圖片描述

CXF的結構:
在這裏插入圖片描述
下載完成後將其解壓到某個目錄下:
在這裏插入圖片描述

基於jdk1.7發佈一個WebService服務
第一步:創建一個Java項目
第二步:創建一個類,加入Webservice註解
第三步:提供一個方法sayHello
第四步:在main方法中調用jdk提供的發佈服務的方法
第五步:訪問服務的wsdl文檔(服務的發佈地址+?wsdl)http://192.168.115.87:8080/hello?wsdl

@WebService
public class HelloService {
	public String sayHello(String name,int i){
		System.out.println("服務端的sayHello方法被調用了。。。。");
		return "helle" + name;
	}
	
	public static void main(String[] args) {
		String address = "http://192.168.115.87:8080/hello";
		Object implementor = new HelloService();
		Endpoint.publish(address, implementor);
	}
}

1客戶端調用
1.jdk中wsimport命令使用
作用:解析wsdl文件,生成客戶端本地代碼
在這裏插入圖片描述
2客戶端調用
1、使用wsimport命令解析wsdl文件生成本地代碼
2、通過本地代碼創建一個代理對象
3、通過代理對象實現遠程調用

/**
 * 1、使用wsimport命令解析wsdl文件生成本地代碼
 * 2、通過本地代碼創建一個代理對象
 * 3、通過代理對象實現遠程調用
 * @author zhaoqx
 *
 */
public class App {
	public static void main(String[] args) {
		HelloServiceService ss = new HelloServiceService();
		//創建客戶端代理對象,用於遠程調用
		HelloService proxy = ss.getHelloServicePort();
		String ret = proxy.sayHello("小明", 10);
		System.out.println(ret);
	}
}

4.2入門案例(服務端開發)
第一步:創建動態web項目
第二步:導入CXF相關jar包

第三步:在web.xml中配置CXF框架提供的一個Servlet

cxf org.apache.cxf.transport.servlet.CXFServlet config-location classpath:cxf.xml cxf /service/*

第四步:在類路徑下提供cxf.xml

<?xml version="1.0" encoding="UTF-8"?>





第五步:開發一個接口和實現類

第六步:在cxf.xml中註冊服務

服務的訪問地址wsdl:

4.3入門案例(客戶端開發)
方式一:使用jdk提供的wsimport命令生成本地代碼完成調用

方式二:使用CXF提供的方式(重點)
第一步:創建Java項目並導入CXF相關jar包
第二步:使用wsimport或者CXF提供wsdl2java生成本地代碼,只需要生成接口文件

第三步:將接口文件複製到項目中

第四步:提供spring配置文件,註冊客戶端代理對象

第五步:讀取spring配置文件,創建spring工廠,從工廠中獲取代理對象,實現遠程調用

5基於CXF開發crm服務
5.1數據庫環境搭建

執行sql腳本:

5.2web項目環境搭建
第一步:創建動態web項目
第二步:導入CXF相關jar包
第三步:配置web.xml

contextConfigLocation
classpath:cxf.xml

org.springframework.web.context.ContextLoaderListener cxf org.apache.cxf.transport.servlet.CXFServlet cxf /service/*

第四步:在類路徑下提供cxf.xml

<?xml version="1.0" encoding="UTF-8"?>






第五步:針對t_customer表創建一個Customer客戶實體類

第六步:開發一個接口和實現類

第七步:配置cxf.xml






<!-- 事務管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 支持事務註解 -->
<tx:annotation-driven transaction-manager="txManager"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="customerService" class="com.itheima.crm.service.CustomerServiceImpl">
	<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>

<!-- 註冊服務 -->
<jaxws:server id="myService" address="/customer">
	<jaxws:serviceBean>
		<ref bean="customerService"/>
	</jaxws:serviceBean>
</jaxws:server>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章