spring調用webservice
spring調用webservice服務是基於Apache的CXF實現的
一、WebService入門
1.1 什麼是WebService
Web service是一個平臺獨立的,低耦合的,自包含的、基於可編程的web的應用程序,可使用開放的XML(標準通用標記語言下的一個子集)標準來描述、發佈、發現、協調和配置這些應用程序,用於開發分佈式的互操作的應用程序。
Web Service技術, 能使得運行在不同機器上的不同應用無須藉助附加的、專門的第三方軟件或硬件, 就可相互交換數據或集成。依據Web Service規範實施的應用之間, 無論它們所使用的語言、 平臺或內部協議是什麼, 都可以相互交換數據。Web Service是自描述、 自包含的可用網絡模塊, 可以執行具體的業務功能。Web Service也很容易部署, 因爲它們基於一些常規的產業標準以及已有的一些技術,諸如標準通用標記語言下的子集XML、HTTP。Web Service減少了應用接口的花費。Web Service爲整個企業甚至多個組織之間的業務流程的集成提供了一個通用機制。
WebService的特點:
- WebService通過HTTP POST方式接受客戶的請求
- WebService與客戶端之間一般使用SOAP協議傳輸XML數據
- 它本身就是爲了跨平臺或跨語言而設計的
1.2 調用網絡上的WebService服務
公共的webservice服務,可以查閱。http://webxml.com.cn/
1.3 SOAP和WSDL概念
1.3.1 SOAP(Simple Object Access Protocol):簡單對象訪問協議
- SOAP作爲一個基於XML語言的協議用於在網上傳輸數據。
- SOAP = 在HTTP的基礎上+XML數據。
- SOAP是基於HTTP的。
SOAP的組成如下:
- Envelope – 必須的部分。以XML的根元素出現。
- Headers – 可選的。
- Body – 必須的。在body部分,包含要執行的服務器的方法。和發送到服務器的數據。
1.3.2 WSDL Web服務描述語言
WSDL(WebService Description Language):web 服務描述語言
就是一個xml文檔,用於描述當前服務的一些信息(服務名稱、服務的發佈地址、服務提供的方法、方法的參數類型、方法的返回值類型等)
二、基於jdk1.7發佈一個WebService服務
2.1 服務端發佈
服務端發佈過程很簡單,只需要在類上加上webservice註解即可
@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);
}
}
2.2 客戶端調用
在控制檯中,定位到一個空文件夾,通過jdk提供的命令,將webservice解析生成本地代碼
/**
* 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);
}
}
三、apache CXF
採用jdk提供的webservice註解對於實際開發中過於困難,因此採用apache提供的CXF來實現
3.1 服務端開發
- 導入CXF相關的jar包
- 在web.xml中配置CXF框架提供的一個Servlet
<!-- 配置CXF框架提供的Servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 通過初始化參數指定CXF框架的配置文件位置 -->
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:cxf.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
- 在類路徑下提供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>
- 開發一個接口和實現類
- 在cxf.xml中註冊服務
3.2 客戶端開發
1、創建Java項目,並導入相關的jar包
2、使用wsimport或者CXF提供wsdl2java生成本地代碼,只需要生成接口文件即可
3、將接口文件複製到項目中
4、提供spring配置文件,註冊客戶端代理對象
5、讀取spring配置文件,創建spring工廠,從工廠中獲取代理對象,實現遠程調用
同樣可以採用註解的方式從spring容器中獲得代理對象
//注入crm代理對象
@Autowired
private ICustomerService proxy;