首先,先來看看兩個簡單的詞彙:CXF、WSDL
一、CXF
Apache CXF = Celtix + XFire,開始叫 Apache CeltiXfire,後來更名爲 Apache CXF 了,以下簡稱爲 CXF。CXF 繼承了 Celtix 和 XFire 兩大開源項目的精華,提供了對 JAX-WS 全面的支持,並且提供了多種 Binding 、DataBinding、Transport 以及各種 Format 的支持,並且可以根據實際項目的需要,採用代碼優先(Code First)或者 WSDL 優先(WSDL First)來輕鬆地實現 Web
Services 的發佈和使用。Apache CXF已經是一個正式的Apache頂級項目。
Apache CXF 是一個開源的 Services 框架,CXF 幫助您利用 Frontend 編程 API 來構建和開發 Services ,像 JAX-WS 。這些 Services 可以支持多種協議,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,並且可以在多種傳輸協議上運行,比如:HTTP、JMS 或者 JBI,CXF 大大簡化了 Services 的創建,同時它繼承了 XFire 傳統,一樣可以天然地和 Spring 進行無縫集成。
特點:靈活部署、支持多種編程語言、代碼生成
鏈接:http://baike.baidu.com/view/2742297.htm
二、WSDL
Web Services Description Language的縮寫,是一個用來描述Web服務和說明如何與Web服務通信的XML語言。爲用戶提供詳細的接口說明書。
WSDL文檔可以分爲兩部分。頂部分由抽象定義組成,而底部分則由具體描述組成。
功能:怎樣向別人介紹你的Web service有什麼功能,以及每個函數調用時的參數呢?你可能會自己寫一套文檔,你甚至可能會口頭上告訴需要使用你的Web service的人。這些非正式的方法至少都有一個嚴重的問題:當程序員坐到電腦前,想要使用你的Web service的時候,他們的工具(如Visual Studio)無法給他們提供任何幫助,因爲這些工具根本就不瞭解你的Web service。解決方法是:用機器能閱讀的方式提供一個正式的描述文檔。Web service描述語言(WSDL)就是這樣一個基於XML的語言,用於描述Web service及其函數、參數和返回值。因爲是基於XML的,所以WSDL既是機器可閱讀的,又是人可閱讀的,這將是一個很大的好處。一些最新的開發工具既能根據你的Web service生成WSDL文檔,又能導入WSDL文檔,生成調用相應Web service的代碼。
鏈接:http://baike.baidu.com/view/160660.htm
然後,我們一起來實現一個簡單的webservice吧
開發前準備
CXF下載地址:http://cxf.apache.org/download.html
此時最精簡的cxf包如下:()
commons-logging-1.1.1.jar
cxf-2.6.2.jar
geronimo-jaxws_2.2_spec-1.1.jar
jaxb-api-2.2.6.jar
neethi-3.0.2.jar
org.apache.cxf.spring.remoting.Jsr181HandlerMapping.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.1.jar
這時萬事具備,只差着手開發
總體包圖:
step one ——
server端:
1、CaribbeanWorld.java
package demo.czd.spring.server;
import javax.jws.WebService;
/**
* @author cai_zedong, 2013-10-15 上午10:03:26
*/
@WebService
public interface CaribbeanWorld {
String sayYeahHeyJude(String text);
}
2、CaribbeanWorldImpl.java
package demo.czd.spring.server;
import javax.jws.WebService;
/**
* @author cai_zedong, 2013-10-15 上午10:07:04
*/
@WebService(endpointInterface = "demo.czd.spring.server.CaribbeanWorld")
public class CaribbeanWorldImpl implements CaribbeanWorld {
public String sayYeahHeyJude(String text) {
return "Yep" + text;
}
}
3、beans.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-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- <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" /> -->
<bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="wrapped" value="true" />
<property name="dataBinding" ref="aegisBean" />
</bean>
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
<jaxws:endpoint id="CollectiveServices" implementor="demo.czd.spring.server.CaribbeanWorldImpl" address="/CaribbeanWorld">
<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean" />
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>
4、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet<rvlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
step two——
client端:
1、ClientTest.java
package demo.czd.spring.client;
import static org.junit.Assert.assertEquals;
import org.apache.cxf.aegis.databinding.AegisDatabinding;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.czd.spring.server.CaribbeanWorld;
/**
* @author cai_zedong, 2013-10-15 上午10:41:39
*/
public final class ClientTest {
private static ClassPathXmlApplicationContext context;
@BeforeClass
public static void beforeClass() {
context = new ClassPathXmlApplicationContext(new String[] { "demo/spring/client/client-beans.xml" });
}
@AfterClass
public static void afterClass() {
context = null;
}
@Test
public void testSayYepWithSpringConfig() throws Exception {
CaribbeanWorld client = (CaribbeanWorld) context.getBean("client");
String response = client.sayYeahHeyJude("Joe");
assertEquals("Hello Joe", response);
}
/**
*
* @throws Exception
*/
@Test
public void testSayHiByCode() throws Exception {
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(CaribbeanWorld.class);
factory.setAddress("http://localhost:8080/demo.czd.spring/server/CaribbeanWorld");
factory.getServiceFactory().setDataBinding(new AegisDatabinding());
CaribbeanWorld client = (CaribbeanWorld) factory.create();
assertEquals("Hello Joe", client.sayYeahHeyJude("Joe"));
}
}
2、client-beans.xml(testSayYepWithSpringConfig()中需要使用的配置文件)
<?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-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
<bean id="serviceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="dataBinding" ref="aegisBean" />
</bean>
<bean id="client" class="demo.spring.czd.server.CaribbeanWorld" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.frontend.ClientProxyFactoryBean">
<property name="serviceFactory" ref="serviceFactoryBean" />
<property name="serviceClass" value="demo.spring..czd.server.CaribbeanWorld" />
<property name="address" value="http://localhost:8080/demo.czd.spring/server/CaribbeanWorld" />
</bean>
</beans>
step three——(S_發佈)
編寫開發完畢,啓動Tomcat,關注有無報錯信息,成功加載運行之後的界面是這樣的:
點擊圖中超鏈接 WSDL : http://server.spring.czd.demo/}CaribbeanWorldImplService
step four——(C_回調)
在命令行輸入命令 wsimport -p [包名] -keep [發佈的服務地址?wsdl] 生成客戶端代碼,如生成本例的客戶端代碼”wsimport -p demo.czd.spring.client -keephttp://localhost:8080/WSServer4czd/CaribbeanWorld?wsdl“,當然,前提是你已經配好了JAVA環境變量。控制檯會顯示
這個時候就可以調用客戶端了,上圖所示
step five——
拷貝在tomcat中webapps底下編譯生成的.java文件到工程目錄下
寫上一段執行代碼,用javaApplication跑一個,輸出以下,即是成功了(代碼貼在由cxf生成的包裏面,文件名如:CaribbeanWorldImplService.java)
... ...
問題:主要一些包的命名規範以及jar包的準確引入,配置文件的時候注意相關版本號即可了
轉:知識積累
小哥獻醜,請大夥多多斧正~