整合spring與cxf,利用cxf編寫webservice

一、下載cxf

下載地址:http://cxf.apache.org/download.html

我下載的是最新版的cxf 2.1.3

 

二、準備工作:在spring環境下使用cxf

1.下載完cxf的包後,至少需要添加如下包,才能正常使用cxf(我一個一個試的,很鬱悶,居然要那麼多,下載的包裏都有):

cxf-2.1.3.jar

commons
-logging-1.1.1.jar
geronimo
-activation_1.1_spec-1.0.2.jar
geronimo
-annotation_1.0_spec-1.1.1.jar
geronimo
-ws-metadata_2.0_spec-1.1.2.jar
geronimo
-stax-api_1.0_spec-1.0.1.jar
geronimo
-jaxws_2.1_spec-1.0.jar
jaxb
-api-2.1.jar
jaxb
-impl-2.1.7.jar
saaj
-api-1.3.jar
wstx
-asl-3.2.6.jar
wsdl4j
-1.6.2.jar
XmlSchema
-1.4.2.jar
xml
-resolver-1.2.jar

 

2.添加如下內容到web.xml

    <!-- CXF WebService -->
    
<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>3</load-on-startup>
    
</servlet>

    
<servlet-mapping>
        
<servlet-name>CXFServlet</servlet-name>
        
<url-pattern>/services/*</url-pattern>
    
</servlet-mapping>

 

3.在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/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" />

添加的內容有三部分:

①xmlns:jaxws="http://cxf.apache.org/jaxws"

②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" />

可以在原有配置文件上修改

 

三、開始使用:編寫服務端WebService

1.添加一個接口,Hello.java:

package com.test.webservice;

import javax.jws.WebService;

@WebService
public interface Hello {
    
public String say(String text);
}

2.實現這個接口,HelloImpl.java:

package com.test.webservice.impl;

import javax.jws.WebService;

import com.ruiri.webservice.Hello;

@WebService(endpointInterface 
= "com.test.webservice.Hello")
public class HelloImpl implements Hello {

    
public String say(String text) {
        System.out.println(text);
        
return "Hello," + text;
    }

}

跟平時使用的接口和類一樣,只是添加了對應的註解

3.在spring的配置文件添加如下內容:

    <jaxws:endpoint id="helloWorld" implementor="com.test.webservice.impl.HelloImpl"
        address
="/HelloWorld" />

這樣,服務端的WebService就做好了,在瀏覽器可以通過 http://站點/services/ 看到可見的service

 

四、編寫客戶端WebService

1.編寫一個接口,這裏還使用先前寫的Hello.java

2.在spring的配置文件添加如下內容:

    <jaxws:client id="helloClient" serviceClass="com.ruiri.webservice.Test1"
        address
="http://站點/services/HelloWorld" />

3.在代碼用調用這個bean:

ServletContext sc = getServletContext();        
WebApplicationContext ac 
= WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        Hello hello 
= (Hello) ac.getBean("helloClient");
        String str = hello
.say("zhang");

這樣,客戶端調用的代碼也有了,配置稍微有些麻煩,配置完後,使用倒是很簡單。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章