一. CXF與spring內建
1.簡介:
CXF是基于JAX-WS實作的,JAX-WS規範是一組XML web services的JAVA API,它使使用者無需編寫複雜的SOAP ENV,WSDL。在 JAX-WS中,一個遠端調用可以轉換為一個基于XML的協定例如SOAP。在使用JAX-WS過程中,開發者不需要編寫任何生成和處理SOAP消息的代碼。JAX-WS的運作時實作會将這些API的調用轉換成為對于SOAP消息。
在伺服器端,使用者隻需要通過Java語言定義遠端調用所需要實作的接口SEI (service endpoint interface),并提供相關的實作,通過調用JAX-WS的服務釋出接口就可以将其釋出為WebService接口。
在用戶端,使用者可以通過JAX-WS的API建立一個代理(用本地對象來替代遠端的服務)來實作對于遠端伺服器端的調用。
2.實作過程:
a) 為CXF設定編譯和開發環境
在http://cxf.apache.org/download.html 下載下傳相應的CXF包,/lib目錄下的jar 檔案引入工程
b) 建立工程,cxfspring,包含WebRoot/WEB-INF目錄
c) 編寫服務
首先寫一個服務接口,例子中的HelloWorld.java。我們要注意的是這個例子使用了JSR181規範中的聲明“@WebService”。
package demo.spring;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
下一步實作這個服務接口,例子中的HelloWorldImpl.java。這個例子代碼中的“@WebService”标簽隻包含一個endpointInterface的屬性,這個屬性讓CXF知道根據哪個接口生成WSDL檔案。這裡有點和我們第一個例子不同,沒有包含ServiceName屬性。這是因為這個屬性會在Spring的配置檔案中聲明,請參考下面的beans.xml這個配置檔案。
package demo.spring;
import javax.jws.WebService;
/**
* javaDoc:http://download.oracle.com/javase/6/docs/api/overview-summary.html
*
* @WebService
* - <wsdl:service name="HelloWorldImplService">
* - <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplServicePort">
* - <soap:address location="http://localhost:8081/TestWebservice/HelloWorld" />
* - </wsdl:port>
* - </wsdl:service>
* targetNamespace: 命名空間,對應tns的定義;沒寫,預設是“http://”+包(package)反過來寫。
* 加了這個屬性,則參數的定義會用<wsdl:import location="http://localhost:8081/TestWebservice/HelloWorldService?wsdl=HelloWorldService.wsdl" ..>,
* 沒加,參數定義就寫在同一個wsdl文檔
* serviceName:對應wsdl文檔,對應:<wsdl:service name="HelloWorldServieImplService">的name;不寫,預設是"類名+Service";
* portName:對應<wsdl:port binding="tns:HelloWorldServieImplServiceSoapBinding" name="HelloWorldServieImplPort">的name;不寫,預設是"類名+ServicePort";
* wsdlLocation: 對應映射的wsdl的實體位址
* endpointInterface: 實作的接口
*
* @Features:調用時,将請求和響應的SOAP資訊列印出來
*/
@WebService(name="HelloWorldImpl",
// targetNamespace="http://spring.demo",
serviceName="HelloWorldImplService",
portName="HelloWorldImplServicePort",
// wsdlLocation = "file:/e:/healthEventRegistration.wsdl",
endpointInter)
//@Features(features = "org.apache.cxf.feature.LoggingFeature")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "Hello " + text;
}
}
d) 在spring中聲明服務的Bean
CXF中包含對Spring2.0很好的支援。對于JAX-WS這一類配置,我們有<jaxws:endpoint>bean作為服務端節點的配置說明。
建立一個”beans.xml”檔案在我們項目的WEB-INF路徑下,注意這個檔案的例子可以在“D:\cxf\samples\java_first_spring_support\”中找到。
<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.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" />
<jaxws:endpoint
id="helloWorld"
implementor="demo.spring.HelloWorldImpl"
address="/HelloWorld" />
</beans>
<jaxws:endpoint>有三個屬性id, implementor和address。
“id”指定這個Bean在Spring上下文中唯一的辨別。
“implementor”指定了這個Web Service的實作類。
“address”指定了服務在Web伺服器上釋出的位址。這個位址可以包含ip和端口的完整位址,也可以是隻包含相對路徑的位址。
e) 配置web.xml
配置的過程中需要在web.xml中添加如下兩項:
第一個是Spring的ContextLoaderListerp類會在啟動時加載上面配置beans.xml檔案。我們需要設定context-param節點;第二個是增加一個CXF Servlet。配置檔案如下,
<web-app>
<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</servlet-name>
<display-name>CXF Servlet</display-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>
</web-app>
這裡需要注意的是配置的endpoint的位址(address)屬性必須和在Beans.xml中定義的一緻。盡管例子中沒有在HelloWorldImpl.java中聲明“address“屬性,也需要在實際的配置中有所注意。
如果你在用戶端代碼要使用這個bean,代碼會非常簡單,例子如下
ApplicationContext context = ...; // your Spring ApplicationContext
HellWorld client = (HelloWorld) context.getBean("client");
[color=red][size=large][b]如果朋友們覺得這篇文章對您有用,而且您需要茶葉茶具和零食之類的東東,請大家到下面這家店鋪購買“品潤茶業”,做批發的,價格超便宜,希望大家多多支援!
位址:[url]http://prtea.taobao.com[/url]
請轉載的朋友,把以上文字也帶上,珍惜别人的勞動果實,謝謝![/b][/size]
[/color]