Spring整合CXf WebService總結

        Web service是一個平臺獨立的,低耦合的,自包含的、基於可編程的web的應用程序,可以使用開放的XML(標準通用標記語言下的一個子集)標準來描述、發佈、發現、協調和配置這些應用程序,用於開發分佈式的互操做的應用程序。關於Java webService框架,主要有AXIS、XFire、CXF,還有Java自帶的JAX-WS(必須JDK6以上環境)。java


        SOAP RPC的工做原理:相似於web的請求/響應方式,不一樣之處在於web客戶和web服務器之間傳輸的是HTML數據。在SOAP RPC模式中,SOAP客戶(程序)和SOAP服務之間傳輸的是符合SOAP規範的XML數據。web


Web services使用兩種技術:spring

        XML(標準通用標記語言下的一個子集):XML是在web上傳送結構化數據的偉大方式,Web services要以一種可靠的自動的方式操做數據,HTML(標準通用標記語言下的一個應用)不會知足要求,而XML可使web services十分方便的處理數據,它的內容與表示的分離十分理想;apache


        SOAP:SOAP使用XML消息調用遠程方法,這樣web services能夠經過HTTP協議的post和get方法與遠程機器交互,並且,SOAP更加健壯和靈活易用;編程

其餘像UDDI和WSDL技術與XML和SOAP技術緊密結合用於服務實現。安全


一、AXIS是apache軟件組織對SOAP規範的實現。服務器

SOAP客戶程序——>AXIS API—RPC請求—>AXIS web應用(SOAP服務),此web應用能夠放在Tomcat容器中。app

在AXIS上建立和發佈基於RPC的SOAP服務步驟:框架

a、建立實現SOAP服務的java類;
b、建立SOAP服務的發佈描述文件;
c、經過AXIS的AdminClient客戶程序發佈SOAP服務;
一個web容器中的web應用———遠程訪問———>另外一個web容器中的AXIS應用(SOAP服務)


二、web服務開源框架XFirefrontend

XFire是下一代的java soap開源框架。XFire提供了方便的API,使用這些API能夠開發面向服務(SOA)的程序。XFire是codehaus組織一個web服務開源框架項目,官方主頁爲http://xfire.codehaus.org.

主要特性包括:

a、支持多個重要的webservice標準,包括SOAP、WSDL等。
b、支持JSR181,能夠經過JDK5中的註解配置web服務。
c、支持基於HTTP、JMS等多種協議訪問web服務。
d、支持spring、Pico等多種容器。
e、支持客戶端和服務器代碼生成。


XFire與spring集成(XFire運行在spring容器中)XFire提供了對spring的集成,可配置spring bean來提供web服務。

a、配置spring bean在web.xml文件中增長spring的配置,同時將XFire的bean配置文件加入到spring容器中。
b、配置XFire Servlet在spring容器中,需配置org.codehaus.xfire.spring.XFireSpringServlet類集中處理web服務的servlet請求。
c、配置web服務Bean在配置spring容器中的bean,此時XFire運行在spring容器中,故再也不須要配置servicers.xml文件,而是在applicationContext.xml配置bean。


web服務安全說明

        提供服務安全機制,經過數字簽名(signature)使用私鑰對報文的摘要進行加密,只有報文在傳輸過程當中不被篡改,接收端在進行數字簽名驗證時纔可能成功。雖然數字簽名解決了完整性和不可抵賴性的安全問題,但消息體仍是以明文的方式進行發送,在傳輸過程當中,消息體的內容可能被監視。此時須要加密(encryption)。


三、CXF與Spring完美集成方案

(1)新建Web工程,如CxfDemo,在pom.xml中添加依賴代碼以下:

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>2.6.2</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>2.6.2</version>
</dependency>


定義接口,使用@webservice註解,代碼以下:

@WebService(name="HelloService", targetNamespace="http://www.yoodb.com")  
public interface HelloService {   

    @WebMethod
    @WebResult(name = "result")
    public String sayHello(@WebParam(name = "s")String s);   

}


實現類,@WebService註解表示是要發佈的web服務,endpointInterface參數的值是該服務類對應的接口。

@WebService(name="HelloService", targetNamespace="http://www.yoodb.com",endpointInterface = "com.yoodb.webservice.HelloService")   
public class HelloServiceImpl implements HelloService{   

 public String  sayHello(String s) {   
        System.out.println( "====>>>>" + s);   
        return "sucess";   

    }   
}


配置applicationcontext-spring文件,具體代碼以下:

<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/schema/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="helloServiceImpl" class="com.yoodb.webservice.HelloServiceImpl" />
    <jaxws:endpoint id="hello" implementor="#helloServiceImpl" address="/Hello" />          

</beans>

implementor參數:指明具體的實現類;address參數:指明這個webservice的相對地址。


web.xml文件添加以下配置,具體代碼以下:

<context-param>  
    <param-name>contextConfigLocation</param-name>  
     <param-value>classpath:/applicationcontext-spring.xml</param-value><!--       加載spring配置文件 -->
</context-param>   

<listener>  
   <listener-class>  
          org.springframework.web.context.ContextLoaderListener  
   </listener-class>  
</listener>  

<servlet>  
    <servlet-name>CXFServlet</servlet-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服務器上,發佈webservice工程,輸入http://localhost:8080/CxfDemo/Hello?wsdl


(2)建立客戶端調用WebService服務測試

//建立WebService客戶端代理工廠
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//註冊WebService接口
factory.setServiceClass(HelloService.class);
//設置WebService地址
factory.setAddress(http://localhost:8080/CxfDemo/Hello);
HelloService hello = (HelloService)factory.create();
//調用webservice接口方法
hello.sayHello("hello");//返回sucess

定義接口,代碼以下:

@WebService(name="HelloService", targetNamespace="http://www.yoodb.com")  
public interface HelloService {   

    @WebMethod
    @WebResult(name = "result")   
    public String sayHello(@WebParam(name = "s")String s);   

}


配置applicationcontext-spring文件,具體代碼以下:

<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/schema/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="helloService" class="com.yoodb.webservice.HelloService" factory-bean="helloClientFactory" factory-method="create"/>
    <bean id="helloClientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
            <property name="serviceClass" value="com.yoodb.webservice.HelloService" />
            <property name="address" value="http://localhost:8080/CxfDemo/Hello" />
    </bean>
</beans>


在Action方法中引用@Resource註解,代碼以下:

@Resource(name="helloService")
private HelloService helloService;

此時action中調用此HelloService中的方法就能夠獲取到數據了。

相關文章
相關標籤/搜索